英文:
Why is my ContentDialog in WinUI 3 displaying empty even though my XAML has a TextBox in it?
问题
以下是您要翻译的代码部分:
// C# 代码
private async void MyMenu_Click(object sender, RoutedEventArgs e)
{
ContentDialog dialog = new ContentDialog()
{
XamlRoot = this.Content.XamlRoot,
Title = "My Dialog",
Content = new MyContentDialog(),
PrimaryButtonText = "OK",
CloseButtonText = "Cancel"
};
ContentDialogResult result = await dialog.ShowAsync();
}
// MyContentDialog 类的代码
namespace myapp
{
public sealed partial class MyContentDialog : ContentDialog
{
public MyContentDialog()
{
this.InitializeComponent();
}
}
}
// MyContentDialog 类的 XAML
<ContentDialog
x:Class="myapp.MyContentDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:myapp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
<Grid>
<TextBox x:Name="MyTextBox" />
</Grid>
</ContentDialog>
希望这能帮助您解决问题。
英文:
WinUI 3 with C#:
I have my app's main Window
class that displays a simple dialog when a menu is clicked:
private async void MyMenu_Click(object sender, RoutedEventArgs e)
{
ContentDialog dialog = new ContentDialog()
{
XamlRoot = this.Content.XamlRoot,
Title = "My Dialog",
Content = new MyContentDialog(),
PrimaryButtonText = "OK",
CloseButtonText = "Cancel"
};
ContentDialogResult result = await dialog.ShowAsync();
}
This is the code-behind for the MyContentDialog
class:
namespace myapp
{
public sealed partial class MyContentDialog : ContentDialog
{
public MyContentDialog()
{
this.InitializeComponent();
}
}
}
And here is the XAML for the MyContentDialog
class:
<ContentDialog
x:Class="myapp.MyContentDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:myapp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
<Grid>
<TextBox x:Name="MyTextBox" />
</Grid>
</ContentDialog>
Seems pretty basic, right? So why is my dialog appearing like this with no TextBox
in it? It doesn't matter what UI controls I add to the XAML, I can't get anything to appear. Why?
答案1
得分: 1
以下是翻译好的部分:
在这里,您已经设置了“Content”:
<ContentDialog
x:Class="myapp.MyContentDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:myapp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Style="{StaticResource DefaultContentDialogStyle}">
<!-- Content -->
<Grid>
<TextBox x:Name="MyTextBox" />
</Grid>
</ContentDialog>
因此,在这里覆盖了“Content”。您只需删除设置“Content”的代码。
MyContentDialog dialog = new()
{
XamlRoot = this.Content.XamlRoot,
Title = "My Dialog",
//Content = new MyContentDialog(),
PrimaryButtonText = "OK",
CloseButtonText = "Cancel"
};
英文:
You are already setting the Content
here:
<ContentDialog
x:Class="myapp.MyContentDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:myapp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Style="{StaticResource DefaultContentDialogStyle}">
<!-- Content -->
<Grid>
<TextBox x:Name="MyTextBox" />
</Grid>
</ContentDialog>
So, you are overriding the Content
here. You just need to remove the code setting the Content
.
MyContentDialog dialog = new()
{
XamlRoot = this.Content.XamlRoot,
Title = "My Dialog",
//Content = new MyContentDialog(),
PrimaryButtonText = "OK",
CloseButtonText = "Cancel"
};
答案2
得分: 0
User Andrew KeepCoding 在部分回答中引导了我走正确的方向,但这里是完全修正后的代码。
我需要直接将 MyContentDialog
类作为对话框调用,移除 Content = new MyContentDialog()
行,并添加一行来更新样式,使其看起来像默认样式。
MyContentDialog dialog = new MyContentDialog()
{
XamlRoot = this.Content.XamlRoot,
Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style,
Title = "My Dialog",
PrimaryButtonText = "OK",
CloseButtonText = "Cancel"
};
ContentDialogResult result = await dialog.ShowAsync();
英文:
User Andrew KeepCoding led me in the right direction with his partial answer, but here is the fully corrected code.
I needed to call the MyContentDialog
class as a dialog directly, remove the Content = new MyContentDialog()
line, and add a line to update the style to look like the default style.
MyContentDialog dialog = new MyContentDialog()
{
XamlRoot = this.Content.XamlRoot,
Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style,
Title = "My Dialog",
PrimaryButtonText = "OK",
CloseButtonText = "Cancel"
};
ContentDialogResult result = await dialog.ShowAsync();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论