windows - 在 Windows/UWP/C++ 中使用单击处理程序动态创建按钮
问题描述
我在创建按钮并将其添加到列表视图的方法中有以下代码。但是,当您使用 C++ 而不是 C# 进行编码时,不清楚如何向按钮添加单击事件处理程序。
Button ^ nb = ref new Button();
nb->Content = ref new Platform::String(name.c_str());
nb->Click // what goes here???
DeviceList->Items->Append(nb);
照明?谢谢你。
解决方案
使用 C++ 编码时如何向按钮添加单击事件处理程序
您需要创建一个绑定OnClick
回调方法的新 RoutedEventHandler 实例,然后使用+=
组合Click
事件。当然,您也可以在输入字符Tab后按快捷键。+=
MainPage::MainPage()
{
InitializeComponent();
Button ^btn = ref new Button();
btn->Content = "TestBtn";
btn->Click += ref new Windows::UI::Xaml::RoutedEventHandler(this, &App5::MainPage::OnClick);
RootLayout->Children->Append(btn);
}
void App5::MainPage::OnClick(Platform::Object ^sender, Windows::UI::Xaml::RoutedEventArgs ^e)
{
throw ref new Platform::NotImplementedException();
}
推荐阅读
- javascript - 更改 innerHTML 不会更新页面
- google-app-engine - 将 queue.xml 部署到新的非默认版本不会创建队列
- python - 如何使用单个摄像头并行运行两个视频帧
- azure-devops - 无法将工作项分配给 Azure DevOps 中的版本
- asp.net-core - 具有自定义需求/处理程序的 .Net 核心授权
- video - 如何让 ffmpeg 从文件列表中连接 1 秒?
- excel - 更改熊猫中的索引列文本
- r - 使用POSIXct时如何避免时区异常变化?
- r - R Shiny - 动态创建的 actionButtons 要么一次全部触发(observeEvent),要么根本不触发(eventReactive)
- mysql - 在 MySql 中,如何查询表以查看一列是否有另一列的两个相似条目?