Skip to content

Proposal: Synchronous ContentDialog #10436

@WindowsNT

Description

@WindowsNT

The idea is that non WinUI3 apps that would switch to WinUI are using MessageBox and DialogBox which are blocking calls; The caller expects a return value.

Trying to create another message loop doesn't work, it crashes on return:

void MainPage::ShowDialogBlocking(winrt::Microsoft::UI::Xaml::Window const& mainWindow)
 {
     auto dialog =  ... // ContentDialog
     auto dispatcherQueue = mainWindow.DispatcherQueue();

     auto as = dialog.ShowAsync();
     as.Completed([&](auto const&, auto)
         {
             dispatcherQueue.EnqueueEventLoopExit();
         });

     dispatcherQueue.RunEventLoop();
 }

If this could work then non WinUI3 apps will have an easier time in transiting to WinUI3.

Note that the following seems to work but I'm not sure it is reliable.

    void MainPage::ShowDialogBlocking()
    {
        auto dialog = ... // ContentDialog
        ContentDialogResult res;
        bool Completed = 0;
        auto as = dialog.ShowAsync();
        as.Completed([&](auto const& sender, auto)
            {
    	        res = sender.as<winrt::Windows::Foundation::IAsyncOperation<ContentDialogResult>>().GetResults(); 
                Completed = 1;
                

            });

        while (!Completed)
        {
            MSG msg;
            if (GetMessage(&msg, 0,0,0))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions