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?
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"
};