microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.33k stars 676 forks source link

ContentDialog defined in Xaml Resource section not showing #6668

Open HO-COOH opened 2 years ago

HO-COOH commented 2 years ago

Describe the bug

<Page
    x:Class="UwpTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UwpTest"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Page.Resources>
        <ContentDialog x:Name="MyDialog" Title="Ttitle"
                       PrimaryButtonText="PrimaryButton" SecondaryButtonText="SecondaryButton"
                       DefaultButton="Primary" Content="Content">
        </ContentDialog>
    </Page.Resources>

    <Grid>
        <Button x:Name="Default" Content="Click me" Click="Default_Click"/>
    </Grid>
</Page>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private async void Default_Click(object sender, RoutedEventArgs e)
        {

            MyDialog.Visibility = Visibility.Visible;
            MyDialog.XamlRoot = this.Content.XamlRoot;
            MyDialog.StartBringIntoView();
            await MyDialog.ShowAsync();
        }
    }

Steps to reproduce the bug

After pressing the button, the background turns darker, but no dialog is showing, nor does it appears in the visual tree. image

Expected behavior

Dialog shows.

Screenshots

No response

NuGet package version

None

Windows app type

Device form factor

Desktop

Windows version

Windows 11

Additional context

No response

StephenLPeters commented 2 years ago

@HO-COOH does this same code work if the content dialog is defined in the tree?

HO-COOH commented 2 years ago

@StephenLPeters If you mean directly put the ContentDialog inside the Grid, no, it does not appear either.

StephenLPeters commented 2 years ago

In that case I think there must be something wrong with how you are attempting to show the dialog. Looking at the code you pasted the call to MyDialog.StartBringIntoView(); is suspect to me. You should only need to set the xaml root and call show async.

HO-COOH commented 2 years ago

Removing that MyDialog.StartBringIntoView(); line does not show either.

ojhad commented 2 years ago

@HO-COOH what version of WinUI are you using?

HO-COOH commented 2 years ago

The default WinUI that bundles with Windows 11, I think. I did not use anything from Microsoft.UI.Xaml.Controls. (Updated in the issue.)

ojhad commented 2 years ago

This does look like a bug, I can reproduce it. I suggest trying to define the contentdialog in the tree or creating it in code behind. Those two methods worked for me. Also in SystemXaml defining the XamlRoot is not necessary.

Ebola-Chan-bot commented 2 years ago

Same issue encountered.

<Page
    x:Class="App2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App2"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Page.Resources>
        <ContentDialog x:Name="Dialog" PrimaryButtonText="Confirm" CloseButtonText="Cancel"/>
    </Page.Resources>
    <Button x:Name="ShowDialog" Content="Show Dialog"/>
</Page>
Public NotInheritable Class MainPage
    Inherits Page

    Private Sub ShowDialog_Click(sender As Object, e As RoutedEventArgs) Handles ShowDialog.Click
        Call Dialog.ShowAsync()
    End Sub
End Class
HO-COOH commented 1 year ago

To avoid getting closed, this issue still exists in WinUI 2.8.5. @StephenLPeters Why are you adding a WinUI3 label but not a Winui2 label? This issue is reproducible with both WinUI3 AND WinUI2