microsoft-ui-xaml: ContentDialog fails to display WinUI 3 Preview 1 and .NET 5 Desktop
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:
-
Create new Blank App, Packaged (WinUI in Desktop) app
-
Open MainWindow.xaml.cs.
-
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(); } -
Run the app
-
Click the Click Me button and observe the exception.
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
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 9
- Comments: 18 (6 by maintainers)
Confirmed the above.
I updated the myButton_Click method to:
The ContentDialog is then displayed as expected.
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 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.
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.
@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.
Adding my two cents here:
As of 2025, WindowsAppSDK v1.5.24, I have run into this VisualTree/XamlRoot issue recently on a WinUI3 project in VS2022. When running from the debugger the ContentDialogs show themselves without issue, however if I run the app’s native assembly (i.e. unpackaged) the ContentDialogs will not show and will crash the application if not for the try/catch I added.
The exception is misleading…
My Project Repo
I have a folder called Dialogs in the main project where I create each <ContentDialog> based on its function. These individual dialogs are then invoked based on what the MainWindow/MainPage needs to prompt the user with. This is a common setup for large projects.
The actual cause of my exception had nothing to do with the XamlRoot or the Dispatcher or the VisualTree, but was actually the PrimaryButton and DefaultButton style. After removing any reference to a style from the <ContentDialog> header, the issue went away.
Very strange, I’m sure someone will explain that the generic styles in WinUI3 make some weird call to some other function that then violates some other thing, but in hindsight I wish MS would work on their exception handling in the WinSDK and make it less cryptic. The actual FirstChanceException contains nothing, not even a type - I was only able to glean the text above with the help of my good buddy
Environment.StackTrace.With all of that said, it still doesn’t explain why it works from within VS2022, but fails when run outside the IDE. I’m guessing VS2022 is ensuring some form of UI context or is masking some other threading issue.
Shouldn’t ContentDialog in a Desktop app create a modeal top-level window (and so not need the XamlRoot to be set)?
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.
Thanks @jtbrower for the feedback. Let’s ping @anawishnoff who driving the new WinUI documentation.