Call Windows open popup/alert/dialog platform specific code

vanKraster 181 Reputation points

I would like to open from platform specific code an alert in Windows.
I know I can do it from await Application.Current.MainPage.DisplayAlert("dad", "dsadas", "cancel"); but I want it from platform specific.
So I have implemented a service in Windows and I have tryied this:

MessageDialog msg = new MessageDialog("No Internet");
await msg.ShowAsync();

and this, but always it shows me an internal error. How can I open a popup in Windows from platform specific code ?

  var alertDialog = new AlertDialog
                        Content = "asda",
                        Title = "title",
                        VerticalScrollBarVisibility = Microsoft.UI.Xaml.Controls.ScrollBarVisibility.Auto,
                        FlowDirection = Microsoft.UI.Xaml.FlowDirection.LeftToRight,
                        PrimaryButtonText = "dasd",
                        SecondaryButtonText = "adsad"
                    await alertDialog.ShowAsync();
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,996 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 37,186 Reputation points Microsoft Vendor


    I noticed that you're using the WinUI2 API calling method, while MAUI programs are based on WinUI3 on Windows, and the way MessageDialog is called is slightly different between them.

    Please refer to User interface migration (including WinUI 3) - MessageDialog, and Pickers to get more details.

    The following is a code examples:

    using var process = Process.GetCurrentProcess();
    var hwnd = process.MainWindowHandle;
    var showDialog = new MessageDialog("Message here");
    WinRT.Interop.InitializeWithWindow.Initialize(showDialog, hwnd);
    await showDialog.ShowAsync();

    Best Regards,

    Alec Liu.

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful