microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.36k stars 678 forks source link

ContentDialog fails to display WinUI 3 Preview 1 and .NET 5 Desktop #2504

Closed darenm closed 1 year ago

darenm commented 4 years ago

Describe the bug While trying out WinUI3 and .NET 5 desktop app, I tried to display a ContentDialog both in code and via a XAML definition. In both circumstances I received the the following exception:

System.ArgumentException
  HResult=0x80070057
  Message=Value does not fall within the expected range.
  Source=WinRT.Runtime
  StackTrace:
   at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32 hr)
   at ABI.Microsoft.UI.Xaml.Controls.IContentDialog.ShowAsync()
   at Microsoft.UI.Xaml.Controls.ContentDialog.ShowAsync()
   at N5UI3Test.MainWindow.<myButton_Click>d__1.MoveNext() in D:\D-Repos\N5UI3Test\N5UI3Test\N5UI3Test\MainWindow.xaml.cs:line 42

Steps to reproduce the bug

Steps to reproduce the behavior:

  1. Create new Blank App, Packaged (WinUI in Desktop) app
  2. Open MainWindow.xaml.cs.
  3. Update the myButton_Click method to the following:

    private async void myButton_Click(object sender, RoutedEventArgs e)
    {
       myButton.Content = "Clicked";
       var cd = new ContentDialog
       {
           Title = "Button Clicker!",
           Content = "You clicked the button!",
           CloseButtonText = "Ok"
       };
       var result = await cd.ShowAsync();
    }
  4. Run the app
  5. Click the Click Me button and observe the exception.

Expected behavior

I expect to see the ContentDialog displayed.

Screenshots

image

Version Info

Windows 10 Version 1909 (18363.836) Microsoft Visual Studio Enterprise 2019 Preview Version 16.7.0 Preview 1.0 Desktop PC 2x 4K monitors Microsoft.NETCore.App 5.0.0-preview.4.20251.6

NuGet package version: Microsoft.WinUI 3.0.0-preview1.200515.3

Windows 10 version Saw the problem?
Insider Build (xxxxx)
November 2019 Update (18363) Yes
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 Yes
Mobile
Xbox
Surface Hub
IoT

Additional context

JesseCol commented 4 years ago

I expect the fix here is that for desktop apps the app needs to set ContentDialog.XamlRoot before showing the dialog. But we really need a better error message.

darenm commented 4 years ago

Confirmed the above.

I updated the myButton_Click method to:

private async void myButton_Click(object sender, RoutedEventArgs e)
{
    myButton.Content = "Clicked";
    var cd = new ContentDialog
    {
        Title = "Button Clicker!",
        Content = "You clicked the button!",
        CloseButtonText = "Ok"
    };

    cd.XamlRoot = this.Content.XamlRoot;
    var result = await cd.ShowAsync();

    //var cd = new ContentDialogExample();
    //await cd.ShowAsync();
}

The ContentDialog is then displayed as expected.

JesseCol commented 4 years ago

Great, thanks for the quick validation! Let's still leave this open to track improving the error message.

jtbrower commented 4 years ago

I stumbled into this one moments ago, thank you @darenm for reporting this so I could quickly apply the fix suggested above.

It seems that documentation and samples might need explicit updates. I am new to both UWP/WinUI coming from about 13 years in WPF. When digging for samples I came across the following resources, but I don't know which ones will need updated and which ones are specific to legacy UWP and should remain the same. For that reason I didn't suggest changes at any of these locations, but will if the powers-to-be want me to.

marb2000 commented 4 years ago

Thanks @jtbrower for the feedback. Let's ping @anawishnoff who driving the new WinUI documentation.

MikeHillberg commented 4 years ago

Shouldn't ContentDialog in a Desktop app create a modeal top-level window (and so not need the XamlRoot to be set)?

marb2000 commented 4 years ago

Good point Mike.

Opening a discussion: https://github.com/microsoft/microsoft-ui-xaml/issues/2848

anawishnoff commented 4 years ago

Thanks for the suggestions @jtbrower! We are definitely looking into creating more samples and docs for WinUI3 as it matures. I know that the scenario of coming from WPF -> WinUI is going to be a common one, so I'll definitely be pushing for docs that highlight key feature differences and/or a getting started guide coming from WPF.

We'll be continuing to add new and useful documentation about WinUI 3 throughout the year, and new docs will not necessarily have to be tied to releases.

jtbrower commented 4 years ago

@anawishnoff I agree that there could be a big wave of developers evaluating WinUI as a UI stack replacement for WPF. Since WPF is the goto framework for MVVM backed desktop applications, I imagine that there are a lot of developers out there who never migrated to UWP because of the vast differences in security restrictions as well as the Windowing model. Now that the WinUI team is focusing on Desktop applications, it finally gives us WPF centric developers some of the tools we need to move to a more performant version of XAML. It may even convince some of the larger WinForms application developers to finally take the plunge and move over to XAML.

I will continue to document my journey and share it back with the team to help improve the transition for other WPF developers.

tuggernuts commented 4 years ago

Hi, @anawishnoff I'd like to agree with and add to @jtbrower's comment.

I went from WPF, to UWP, and now evaluating WinUI for Desktop (.net5) for similar reasons. The UWP -> WinUI jump will be common, and not simple, I am finding.

For those of us 'evaluating,' there is a paucity of working samples and even less documentation. It's practically impossible to evaluate anything, beyond the most simple 'Hello World' app. So I'd respectfully ask that any docs and samples that exist, in whatever state, that they be put somewhere where developers can find and use them. Right now, I'm just picking through the issues here and screengrabs from whatever videos (i,e, ignite) I can find.

marb2000 commented 4 years ago

Thanks for your feedback @tuggernuts, I know @anawishnoff is working in a plan to improve the documentation, but it won't be landed soon. I hope the Preview 3 (Nov 2020) allows us to update the samples and create even more docs specific for WinUI 3. I will make public the sample I show at Ignite as well with the Preview 3 release too.

HebuSan commented 3 years ago

Hi, Just in case, this issue still exists with WinUi 3 (Windows App SDK 0.81) in desktop mode. This post is the only place where I could find a workaround. Thx @darenm. I can confirm the exception thrown is not really clear.

Also, I tried using ContentDialog because I faced weird issues while trying to use a MessageDialog in my code. Using MessageDialog leads to the following exception: "system.runtime.interopservices.comexception the handle is invalid"

In this link it is said: "You should use MessageDialog only when you are upgrading a Universal Windows 8 app that uses MessageDialog, and need to minimize changes. For new apps in Windows 10, we recommend using the ContentDialog control instead."

But maybe this should be fixed as well, if we can still refer to the MessageDialog Class.

dedaap72 commented 2 years ago

This issue still exists in WinUI3, with the same vague error message. And agreeing with what others have written, documentation for WinUI is sparse. Also, with the different frameworks now it's hard to find the right way for the framework you're using. Coming from WPF but not having used UWP before, it's sometimes unclear where to look: WinUI? WinRT? UWP? Some things from WPF work in WinUI but others don't, so what's the alternative?

Gabboxl commented 2 years ago

This issue still exists in WinUI3, with the same vague error message. And agreeing with what others have written, documentation for WinUI is sparse. Also, with the different frameworks now it's hard to find the right way for the framework you're using. Coming from WPF but not having used UWP before, it's sometimes unclear where to look: WinUI? WinRT? UWP? Some things from WPF work in WinUI but others don't, so what's the alternative?

I completely agree. Just got into WinUI 3 and everything is so confusing, especially the documentation even for basic things like these. Hope things will be clearer in the future.

AtosNicoS commented 2 years ago

Is the solution by @darenm supposted to be the common way to handle ContentDialogs or is this just a workaround for a bug that must be fixed in the framework? I am currently confused on what to use for WinUI3, as it is so buggy.

kmgallahan commented 2 years ago

@marb2000

The WinUI 3 docs for ContentDialog mention setting xamlroot, however as the text was copied from the UWP / WinUI 2 docs it erroneously hints that it isn't always required when using WinUI 3 / Microsoft.UI.Xaml, which is not the case.

In my opinion WinUI 2 should not be referenced while browsing WinUI 3 / Microsoft.UI.Xaml docs as seen in the ContentDialog "Tip" bubble.

Further, old code samples like the ones shown before xamlroot is mentioned should be updated to reflect the current state of working with ContentDialog.

Although it would take a fair amount of time, not cleaning out old legacy UWP / WinUI 2 code, links, and references from the WinUI 3 docs is going to perpetually confuse newcomers.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days.