我试图在UWP C ++应用中使用ContentDialog^
。使用MessageDialog^
您可以轻松设置触摸时调用的按钮和命令Windows::UI::Popups::UICommand
MessageDialog^ msg = ref new MessageDialog("Data changed - Save?");
msg->Title = "Warning";
// Add commands and set their callbacks.
UICommand^ continueCommand = ref new UICommand("Yes", ref new UICommandInvokedHandler(this, &MainPage::CommandInvokedHandler));
UICommand^ upgradeCommand = ref new UICommand("No", ref new UICommandInvokedHandler(this, &MainPage::CommandInvokedHandler));
UICommand^ cancelCommand = ref new UICommand("Cancel", ref new UICommandInvokedHandler(this, &MainPage::CommandInvokedHandler));
然后您可以轻松访问
发送给CommandInvokedHandler
的值
void MainPage::CommandInvokedHandler(Windows::UI::Popups::IUICommand^ command)
{
// Display message
if (command->Label == "Yes") {
Save();
} else if (command->Label == "No") {
Skip();
} else if (command->Label == "Cancel") {
//do nothing
}
}
但是,内容对话框的工作方式完全不同。它是这样创建的
TextBox^ inputTextBox = ref new TextBox();
inputTextBox->AcceptsReturn = false;
inputTextBox->Height = 32;
ContentDialog^ dialog = ref new ContentDialog();
dialog->Content = inputTextBox;
dialog->Title = "Rename";
dialog->IsSecondaryButtonEnabled = true;
dialog->PrimaryButtonText = "Ok";
dialog->SecondaryButtonText = "Cancel";
dialog->ShowAsync();
我要么设置属性dialog->PrimaryButtonCommand
,因为某些(未知的奇怪)原因会使用完全不同的Windows::UI::Xaml::Input::ICommand
...
或者我应该使用dialog->PrimaryButtonClick
?
我很难在任何地方找到任何C ++示例,而且文档并没有清除任何内容。
答案 0 :(得分:1)
您可以在PrimaryButtonCommand中使用PrimaryButtonClick或ContentDialog。点击主按钮时将调用它们。它们之间的区别在于PrimaryButtonClick
是event,但PrimaryButtonCommand
是类型为ICommand的属性。请注意ICommand与MessageDialog
中使用的UICommand不同,它们完全是两回事。
ICommand更多地用于XAML绑定。要使用它,我们将实现ICommand接口,然后在视图模型中使用。有关详细信息,请参阅Executing commands in a view model。
在您的情况下,当您在代码隐藏中创建内容对话框时,我认为您可以使用如下所示的事件处理程序方法订阅PrimaryButtonClick
事件:
void MainPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
TextBox^ inputTextBox = ref new TextBox();
inputTextBox->AcceptsReturn = false;
inputTextBox->Height = 32;
ContentDialog^ dialog = ref new ContentDialog();
dialog->Content = inputTextBox;
dialog->Title = "Rename";
dialog->IsSecondaryButtonEnabled = true;
dialog->PrimaryButtonText = "Ok";
dialog->SecondaryButtonText = "Cancel";
dialog->PrimaryButtonClick += ref new Windows::Foundation::TypedEventHandler<Windows::UI::Xaml::Controls::ContentDialog ^, Windows::UI::Xaml::Controls::ContentDialogButtonClickEventArgs ^>(this, &MainPage::OnPrimaryButtonClick);
dialog->ShowAsync();
}
void MainPage::OnPrimaryButtonClick(Windows::UI::Xaml::Controls::ContentDialog ^sender, Windows::UI::Xaml::Controls::ContentDialogButtonClickEventArgs ^args)
{
// Do something useful here
}