WinUI 3 UWP - 对话框
WinUI 3 UWP - Dialog
我正在 WinUI3 UWP 中为 Windows 11 开发一个新应用程序,我想显示一个对话框以提供安全操作,就像 Microsoft Docs 的这个示例:https://docs.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();
}
当用户单击我的 MainPage.xaml 按钮时显示该对话框,当用户单击“确定”时 returns 到 MainPage.xaml,但是当我 运行 我的程序给我这个错误:“XamlRoot 必须显式设置为 unparented 弹出窗口”
我该如何解决?
谢谢!
看来您正在开发 WinUI3 应用程序。正如@Raymond Chen 提到的,您必须将 XamlRoot property
添加到 ContentDialog.
Xaml:
<StackPanel x:Name="MyPanel" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button x:Name="myButton" Click="myButton_Click">Click Me</Button>
</StackPanel>
代码隐藏:
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();
}
我正在 WinUI3 UWP 中为 Windows 11 开发一个新应用程序,我想显示一个对话框以提供安全操作,就像 Microsoft Docs 的这个示例:https://docs.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();
}
当用户单击我的 MainPage.xaml 按钮时显示该对话框,当用户单击“确定”时 returns 到 MainPage.xaml,但是当我 运行 我的程序给我这个错误:“XamlRoot 必须显式设置为 unparented 弹出窗口”
我该如何解决?
谢谢!
看来您正在开发 WinUI3 应用程序。正如@Raymond Chen 提到的,您必须将 XamlRoot property
添加到 ContentDialog.
Xaml:
<StackPanel x:Name="MyPanel" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button x:Name="myButton" Click="myButton_Click">Click Me</Button>
</StackPanel>
代码隐藏:
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();
}