【问题标题】:UWP C++ ContentDialog PrimaryButtonCommand or PrimaryButtonClickUWP C++ ContentDialog PrimaryButtonCommand 或 PrimaryButtonClick
【发布时间】:2017-10-23 20:04:27
【问题描述】:

我正在尝试在 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++ 示例,但文档并没有说明任何内容。

【问题讨论】:

    标签: uwp c++-cx


    【解决方案1】:

    您可以在ContentDialog 中使用PrimaryButtonCommandPrimaryButtonClick。当主按钮被点击时,它们都会被调用。它们之间的区别在于PrimaryButtonClickevent,而PrimaryButtonCommand 是类型为ICommand 的属性。请注意ICommandMessageDialog 中使用的U​ICommand 不同,它们完全是两个不同的东西。

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多