I am develop a new app for Windows 11 in WinUI3 UWP, and I want show a dialog to provide a safe action like in this example of Microsoft Docs: https://learn.microsoft.com/en-us/windows/apps/design/controls/dialogs-and-flyouts/dialogs
private async void DisplayNoWifiDialog()
{
ContentDialog noWifiDialog = new ContentDialog
{
Title = "No wifi connection",
Content = "Check your connection and try again.",
CloseButtonText = "Ok"
};
ContentDialogResult result = await noWifiDialog.ShowAsync();
}
When the user click in button on my MainPage.xaml show that Dialog and when user click on "ok" returns to MainPage.xaml, but when I run my program give me this error: "XamlRoot must be explicitly set for unparented popup"
How can I resolve this?
Thank you!
It seems like that you are developing a WinUI3 app. As @Raymond Chen mentioned, you will have to add the XamlRoot property
to the ContentDialog.
Xaml:
<StackPanel x:Name="MyPanel" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button x:Name="myButton" Click="myButton_Click">Click Me</Button>
</StackPanel>
Code-behind:
private async void myButton_Click(object sender, RoutedEventArgs e)
{
ContentDialog noWifiDialog = new ContentDialog
{
Title = "No wifi connection",
Content = "Check your connection and try again.",
CloseButtonText = "Ok"
};
//set the XamlRoot property
noWifiDialog.XamlRoot = MyPanel.XamlRoot;
ContentDialogResult result = await noWifiDialog.ShowAsync();
}