Windows 10 UWP 获取内容对话框在移动屏幕上垂直居中

Windows 10 UWP get content dialog to center in screen vertically on mobile

我在 Windows 10 Mobile 上的整个应用程序中使用 ContentDialogs。默认情况下,ContentDialog 和 MessageDialog 似乎更喜欢锚定到屏幕顶部。但是,我看到一些应用程序垂直显示在屏幕中间,因此它显示为条带。我不知道如何将它垂直放置在中间。这个我试过了

cd.VerticalAlignment = VerticalAlignment.Center;

其中 cd 是我的 ContentDialog 实例,但这不起作用。它仍然锚定在 window.

的顶部

谢谢!

您应该为 ContentDialog 覆盖 Style。我尝试将 VerticalAlignment.Center 设置为 ContainerLayoutRootBackgroundElement,但没有用。因此,将 BackgroundElement 置于 LayoutRoot 的中心行,刚刚为您的 BackgroundElement 添加了新的 RowDefinition 并设置了 Grid.Row="1"

<Grid.RowDefinitions>
    <RowDefinition Height="*" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="*" />
</Grid.RowDefinitions>

查看完整的 ContentDialog 样式。

注释

但要小心,因为这仅适用于移动设备。您应该只将此样式应用于移动平台。

<ContentDialog.Style>
    <Style TargetType="ContentDialog">
        <Setter Property="Foreground" Value="{ThemeResource SystemControlPageTextBaseHighBrush}" />
        <Setter Property="Background" Value="{ThemeResource SystemControlBackgroundChromeMediumLowBrush}" />
        <Setter Property="HorizontalAlignment" Value="Center" />
        <Setter Property="VerticalAlignment" Value="Top" />
        <Setter Property="IsTabStop" Value="False" />
        <Setter Property="MaxHeight" Value="{ThemeResource ContentDialogMaxHeight}" />
        <Setter Property="MinHeight" Value="{ThemeResource ContentDialogMinHeight}" />
        <Setter Property="MaxWidth" Value="{ThemeResource ContentDialogMaxWidth}" />
        <Setter Property="MinWidth" Value="{ThemeResource ContentDialogMinWidth}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ContentDialog">
                    <Border x:Name="Container">
                        <Grid x:Name="LayoutRoot">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <Border x:Name="BackgroundElement"
                                    Grid.Row="1"
                                    Background="{TemplateBinding Background}"
                                    FlowDirection="{TemplateBinding FlowDirection}"
                                    BorderThickness="{ThemeResource ContentDialogBorderWidth}"
                                    BorderBrush="{ThemeResource SystemControlForegroundAccentBrush}"
                                    MaxWidth="{TemplateBinding MaxWidth}"
                                    MaxHeight="{TemplateBinding MaxHeight}"
                                    MinWidth="{TemplateBinding MinWidth}"
                                    MinHeight="{TemplateBinding MinHeight}" >
                                <Grid x:Name="DialogSpace" VerticalAlignment="Stretch">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="Auto" />
                                    </Grid.RowDefinitions>
                                    <ScrollViewer x:Name="ContentScrollViewer"
                                        HorizontalScrollBarVisibility="Disabled"
                                        VerticalScrollBarVisibility="Disabled"
                                        ZoomMode="Disabled"
                                        Margin="{ThemeResource ContentDialogContentScrollViewerMargin}"
                                        IsTabStop="False">
                                        <Grid>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="Auto" />
                                                <RowDefinition Height="Auto" />
                                            </Grid.RowDefinitions>
                                            <ContentControl x:Name="Title"
                                            Margin="{ThemeResource ContentDialogTitleMargin}"
                                            Content="{TemplateBinding Title}"
                                            ContentTemplate="{TemplateBinding TitleTemplate}"
                                            FontSize="20"
                                            FontFamily="XamlAutoFontFamily"
                                            FontWeight="Normal"
                                            Foreground="{TemplateBinding Foreground}"
                                            HorizontalAlignment="Left"
                                            VerticalAlignment="Top"
                                            IsTabStop="False"
                                            MaxHeight="{ThemeResource ContentDialogTitleMaxHeight}" >
                                                <ContentControl.Template>
                                                    <ControlTemplate TargetType="ContentControl">
                                                        <ContentPresenter
                                                        Content="{TemplateBinding Content}"
                                                        MaxLines="2"
                                                        TextWrapping="Wrap"
                                                        ContentTemplate="{TemplateBinding ContentTemplate}"
                                                        Margin="{TemplateBinding Padding}"
                                                        ContentTransitions="{TemplateBinding ContentTransitions}"
                                                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                                                    </ControlTemplate>
                                                </ContentControl.Template>
                                            </ContentControl>
                                            <ContentPresenter x:Name="Content"
                                            ContentTemplate="{TemplateBinding ContentTemplate}"
                                            Content="{TemplateBinding Content}"
                                            FontSize="{ThemeResource ControlContentThemeFontSize}"
                                            FontFamily="{ThemeResource ContentControlThemeFontFamily}"
                                            Margin="{ThemeResource ContentDialogContentMargin}"
                                            Foreground="{TemplateBinding Foreground}"
                                            Grid.Row="1"
                                            TextWrapping="Wrap" />
                                        </Grid>
                                    </ScrollViewer>
                                    <Grid x:Name="CommandSpace" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Bottom">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition/>
                                            <ColumnDefinition/>
                                        </Grid.ColumnDefinitions>
                                        <Border x:Name="Button1Host"
                                        Margin="{ThemeResource ContentDialogButton1HostMargin}"
                                        MinWidth="{ThemeResource ContentDialogButtonMinWidth}"
                                        MaxWidth="{ThemeResource ContentDialogButtonMaxWidth}"
                                        Height="{ThemeResource ContentDialogButtonHeight}"
                                        HorizontalAlignment="Stretch" />
                                        <Border x:Name="Button2Host"
                                        Margin="{ThemeResource ContentDialogButton2HostMargin}"
                                        MinWidth="{ThemeResource ContentDialogButtonMinWidth}"
                                        MaxWidth="{ThemeResource ContentDialogButtonMaxWidth}"
                                        Height="{ThemeResource ContentDialogButtonHeight}"
                                        Grid.Column="1"
                                        HorizontalAlignment="Stretch" />
                                    </Grid>
                                </Grid>
                            </Border>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ContentDialog.Style>