Why is my ContentDialog in WinUI 3 displaying empty even though my XAML has a TextBox in it?

huangapple go评论71阅读模式
英文:

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 = &quot;My Dialog&quot;,
        Content = new MyContentDialog(),
        PrimaryButtonText = &quot;OK&quot;,
        CloseButtonText = &quot;Cancel&quot;
    };
    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:

&lt;ContentDialog
    x:Class=&quot;myapp.MyContentDialog&quot;
    xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
    xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
    xmlns:local=&quot;using:myapp&quot;
    xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
    xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;&gt;

    &lt;Grid&gt;
        &lt;TextBox x:Name=&quot;MyTextBox&quot; /&gt;
    &lt;/Grid&gt;
&lt;/ContentDialog&gt;

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?

Why is my ContentDialog in WinUI 3 displaying empty even though my XAML has a TextBox in it?

答案1

得分: 1

以下是翻译好的部分:

在这里,您已经设置了“Content”:

&lt;ContentDialog
    x:Class=&quot;myapp.MyContentDialog&quot;
    xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
    xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
    xmlns:local=&quot;using:myapp&quot;
    xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
    xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
    Style=&quot;{StaticResource DefaultContentDialogStyle}&quot;&gt;

    &lt;!-- Content --&gt;
    &lt;Grid&gt;
        &lt;TextBox x:Name=&quot;MyTextBox&quot; /&gt;
    &lt;/Grid&gt;

&lt;/ContentDialog&gt;

因此,在这里覆盖了“Content”。您只需删除设置“Content”的代码。

MyContentDialog dialog = new()
{
    XamlRoot = this.Content.XamlRoot,
    Title = &quot;My Dialog&quot;,
    //Content = new MyContentDialog(), 
    PrimaryButtonText = &quot;OK&quot;,
    CloseButtonText = &quot;Cancel&quot;
};
英文:

You are already setting the Content here:

&lt;ContentDialog
    x:Class=&quot;myapp.MyContentDialog&quot;
    xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
    xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
    xmlns:local=&quot;using:myapp&quot;
    xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
    xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
    Style=&quot;{StaticResource DefaultContentDialogStyle}&quot;&gt;

    &lt;!-- Content --&gt;
    &lt;Grid&gt;
        &lt;TextBox x:Name=&quot;MyTextBox&quot; /&gt;
    &lt;/Grid&gt;

&lt;/ContentDialog&gt;

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 = &quot;My Dialog&quot;,
    //Content = new MyContentDialog(), 
    PrimaryButtonText = &quot;OK&quot;,
    CloseButtonText = &quot;Cancel&quot;
};

答案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[&quot;DefaultContentDialogStyle&quot;] as Style,
    Title = &quot;My Dialog&quot;,
    PrimaryButtonText = &quot;OK&quot;,
    CloseButtonText = &quot;Cancel&quot;
};
ContentDialogResult result = await dialog.ShowAsync();

huangapple
  • 本文由 发表于 2023年3月1日 14:18:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600167.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定