Reputation: 411
I'm trying to use ContentDialog^
in a UWP C++ app. With MessageDialog^
you can easily set the buttons and commands Windows::UI::Popups::UICommand
they invoke when touched
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));
then you can easily access the value sent to the CommandInvokedHandler
by
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
}
}
However, the content dialog works completely differently. It's created like this
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();
I either need to set the property dialog->PrimaryButtonCommand
that for some (unknown bizarre) reason uses the completely different Windows::UI::Xaml::Input::ICommand
...
Or should I use the dialog->PrimaryButtonClick
?
I'm struggling to find any C++ examples anywhere and the documentation doesn't clear anything up.
Upvotes: 4
Views: 2042
Reputation: 15758
You can either use 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 UICommand used in MessageDialog
, they are totally two different things.
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.
And in your case, as you are creating content dialog in code-behind, so I think you can just subscribe to PrimaryButtonClick
event with an event handler method like the following:
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
}
Upvotes: 4