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:
csharp
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();
}
Expected behavior
I expect to see the ContentDialog displayed.
Screenshots
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
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.
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.
Great, thanks for the quick validation! Let's still leave this open to track improving the error message.
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.
Thanks @jtbrower for the feedback. Let's ping @anawishnoff who driving the new WinUI documentation.
Shouldn't ContentDialog in a Desktop app create a modeal top-level window (and so not need the XamlRoot to be set)?
Good point Mike.
Opening a discussion: https://github.com/microsoft/microsoft-ui-xaml/issues/2848
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.
@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.
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.
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.
Most helpful comment
Confirmed the above.
I updated the myButton_Click method to:
The ContentDialog is then displayed as expected.