Modal ContentDialog WinUI

76 views Asked by At

The idea is to replace MessageBox() in my code with a function that would show a ContentDialog with Yes/No, for example. Since this is an existing code from a big Win32 project to WinUI I can't rewrite everything to cope with a lambda call, for example

ct.ShowAsync().Completed([&]() { ... } ); 

I need a function that would not return until the user has pressed one of the buttons.

What I tried is:

auto sy = ct.ShowAsync();
for (;;)
{
    auto resu = sy.GetResults();
    if (resu != ContentDialogResult::None)
        break;
    MsgWaitForMultipleObjects(0, 0, FALSE, 100, QS_ALLINPUT);
    MSG msg;
    GetMessage(&msg, 0, 0, 0);
    TranslateMessage(&msg);
    DispatchMessageW(&msg);
}

to simulate a "modal" dialog box. It simply stucks my application. I can't also use a thread, because the dialog has to be shown in the main UI.

Any suggestions?

1

There are 1 answers

0
sjb-sjb On

As pointed out by @IInspectable, you need to use the dispatcher if you want your dials to work from off of the UI thread.

One key thing about ContentDialog is that you are only allowed to show one at a time. To do this you have to wait any previously shown dialog before showing the new one. See "Only a single ContentDialog can be open at any time." error while opening another contentdialog

Another important point with Content Dialog is getting it Center in the page correctly. The best way is to embed it in or near the root of your app layout, see ContentDialog not aligning to center on UWP