Closed darenm closed 1 year 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.
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.
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.
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?
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.
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.
@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.
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.
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:
Steps to reproduce the bug
Steps to reproduce the behavior:
Update the myButton_Click method to the following:
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
Additional context