-1

When I try to invoke an MessageDialog my app crashes with the following error message:

 System.Runtime.InteropServices.COMException: 'Invalid Window Identifier. (0x80070578)'

This occurs both on the App.xaml.xs and in an helper I have for that propose.

Code on app.xaml:

var messageDialog = new MessageDialog("Excedeu o limite de tentativas para fazer login.");
var result = await messageDialog.ShowAsync();

Code on helper:

public static async Task ShowAsync(String keyName)
        {
            var msg = LocalizationHelper.GetValue(keyName);
            if (string.IsNullOrWhiteSpace(msg)) msg = keyName;

            var messageDialog = new MessageDialog(msg);
            await messageDialog.ShowAsync();
        }
                  
David Simões
  • 303
  • 4
  • 11

2 Answers2

2

You should use the ContentDialog class to build your dialog experience.

The MessageDialog API is deprecated as stated in the docs:

Use the ContentDialog class to build your dialog experience. Don't use the deprecated MessageDialog API.

mm8
  • 163,881
  • 10
  • 57
  • 88
2

In desktop apps on .NET 5 or later, you'll need to use the InitializeWithWindow.Initialize method on your MessageDialog object before popping the dialog.

This is because desktop apps can have more than one window object, so you'll need to specify the owner window for your dialog.

For example:

var messageDialog = new MessageDialog("example");
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(myWindow);
WinRT.Interop.InitializeWithWindow.Initialize(messageDialog, hwnd);
var result = await messageDialog.ShowAsync();

For more details

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Angela Z
  • 21
  • 1