UWP C++ ContentDialog PrimaryButtonCommand 或 PrimaryButtonClick

UWP C++ ContentDialog PrimaryButtonCommand or PrimaryButtonClick

我正在尝试在 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++ 示例,而且文档没有清除任何内容。

你可以用PrimaryButtonCommand or PrimaryButtonClick in ContentDialog. Both of them will be invoked when the primary button has been tapped. The difference between them is that PrimaryButtonClick is a event, but PrimaryButtonCommand is a property whose type is ICommand. Please note ICommand is not the same as U​ICommand用在MessageDialog,它们完全是两个不同的东西。

ICommand is more used with XAML Binding. To use it, we will implement ICommand interface and then use in view model. For more info, please refer to 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
}