I'm new to WinUI3. I'm trying to create a modal dialog using ContentDialog class, but the async nature confuses me. Suppose, there is an WinUI3 application like shown below. I have two buttons which display the same ContentDialog one in modal the other in modeless way.
winrt::Windows::Foundation::IAsyncAction MainWindow::ShowAlert()
{
ContentDialog dlg;
dlg.XamlRoot(Content().XamlRoot());
dlg.Title(winrt::box_value(L"Title"));
dlg.Content(winrt::box_value(L"Message"));
dlg.PrimaryButtonText(L"OK");
dlg.SecondaryButtonText(L"Cancel");
ContentDialogResult res = co_await dlg.ShowAsync();
_value = (res == ContentDialogResult::Primary ? 1 : 2);
}
void MainWindow::button1Click(IInspectable const&, RoutedEventArgs const&)
{
_value = 0;
// modeless invocation
ShowAlert();
// label displays 0, always
label().Text(winrt::to_hstring(_value));
}
winrt::Windows::Foundation::IAsyncAction MainWindow::button2Click(IInspectable const&, RoutedEventArgs const&)
{
_value = 0;
// modal invocation
co_await ShowAlert();
// label displays either 1 or 2
label().Text(winrt::to_hstring(_value));
co_return;
}
I wonder if there is a way to display the dialog in modal way from the button1Click() handler which returns 'void'. The execution flow in that function will depend on the result of the invocation of modal dialog.
Adding co_await to ShowAlert in button1Click() call results in compiler error:
error C2039: 'promise_type': is not a member of 'std::experimental::coroutine_traits<void,winrt::App1::implementation::MainWindow *,const winrt::implements<D,winrt::App1::MainWindow,winrt::composing,winrt::Microsoft::UI::Xaml::Markup::IComponentConnector>::IInspectable &,const winrt::Microsoft::UI::Xaml::RoutedEventArgs &>'
Thanks in advance!