question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Preview 4] MessageDialog is no longer functional

See original GitHub issue

Describe the bug Showing a message dialog no longer works in a Win32 app using Preview 4

Steps to reproduce the bug Run the following code, for instance in a button click event:

            var dlg = new MessageDialog("Test");
            try
            {
                await dlg.ShowAsync();
            }
            catch (System.Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

See the following exception thrown in the catch:

Invalid window handle. (0x80070578)

Expected behavior Dialog displayed

Version Info

NuGet package version: [Microsoft.WinUI 3.0.0-preview4.210210.4]

Windows app type:

UWP Win32
Yes
Windows 10 version Saw the problem?
Insider Build (xxxxx)
October 2020 Update (19042)
May 2020 Update (19041)
November 2019 Update (18363)
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop
Xbox
Surface Hub
IoT

Additional context Workaround:

        [ComImport]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        [Guid("3E68D4BD-7135-4D10-8018-9FB6D9F33FA1")]
        internal interface IInitializeWithWindow
        {
            void Initialize(IntPtr hwnd);
        }

        [ComImport]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        [Guid("EECDBF0E-BAE9-4CB6-A68E-9598E1CB57BB")]
        internal interface IWindowNative
        {
            IntPtr WindowHandle { get; }
        }


// ...
            var dlg = new MessageDialog("Test");
            IInitializeWithWindow withWindow = dlg.As<IInitializeWithWindow>();
            var handle = this.As<IWindowNative>().WindowHandle;
            try
            {
                withWindow.Initialize(handle);
                await dlg.ShowAsync();
            }
            catch (System.Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

Alternative workaround without requiring knowledge of the current window:

        public static IAsyncOperation<IUICommand> ShowDialogAsync(string content, string title = null)
        {
            var dlg = new MessageDialog(content, title ?? "");
            var handle = GetActiveWindow();
            if (handle == IntPtr.Zero)
                throw new InvalidOperationException();
            dlg.As<IInitializeWithWindow>().Initialize(handle);
            return dlg.ShowAsync();
        }

        [ComImport]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        [Guid("3E68D4BD-7135-4D10-8018-9FB6D9F33FA1")]
        internal interface IInitializeWithWindow
        {
            void Initialize(IntPtr hwnd);
        }

        [DllImport("user32.dll")]
        private static extern IntPtr GetActiveWindow();

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:15 (11 by maintainers)

github_iconTop GitHub Comments

6reactions
dotMortencommented, Jul 30, 2021

Yup I’m aware of the new helper, but it doesn’t really address the problem. If the dialog took the xamlroot as a parameter it would at least be a bit more obvious, perhaps combined with hiding the empty constructor from intellisense (since it needs to stay there for xaml support).

And speaking of that helper: It isn’t even clear what type it takes as a parameter (just any object), nor is there any intellisense to tell me anything: image image

When you don’t hook up the window handle or root (because how are you supposed to know you need to?), you get really really poor error messages, not help messages (it’s not invalid, it’s just not set):

image

At least you can get a bit further with some extension methods, but we shouldn’t have to do this: image

All of this ultimately leads to an extremely frustrating and off-putting developer experience. I feel a bit like beating a dead horse, since I’ve been making this same point over and over. It’s one thing that WinUI “works” and can do what it needs to, but that doesn’t help, if the developer experience is so frustrating we give up before we really get into using it.

5reactions
dotMortencommented, Jul 30, 2021

Remember that MessageDialog is an old Windows 8 era system API

Again just an excuse. WinUI needs a message dialog that fits into the WinUI model of things. We shouldn’t have to be doing these crazy hacks to make basic things work. But we’re currently left with no choice.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MessageDialog Class (Windows.UI.Popups) - UWP
Represents a dialog for showing messages to the user. In a desktop app, before using an instance of this class in a way...
Read more >
QML MessageDialog not shown correctly
I have defined a slot/function in main.qml (onNotifyOperator) to handle the signal. The two are connected in the main() function of my ...
Read more >
MessageDialog QML Type | Qt Labs Platform 6.5.2
The MessageDialog type provides a QML API for native platform message dialogs. A message dialog is used to inform the user, or ask...
Read more >
MessageDialog QML Type | Qt Quick Dialogs
This property holds the informative text that provides a fuller description for the message. Informative text can be used to expand upon the...
Read more >
18. Dialogs — Python GTK+ 3 Tutorial 3.4 documentation
Gtk.MessageDialog is used for most simple notifications. ... method can be used to delete the dialog from memory once it is no longer...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found