Windows UWP ContentDialog去掉阴影(全透明)的实现

一、前言

    在WIndows开发中,使用UWP(Universal WIndows)项目开发过程中,使用ContentDialog 的过程中,我们可能并不满足现有的样式,这时就需要自定义样式。笔者在自定义样式过程中,遇到了一个难题,当使用了自定义的背景之后,发现后面总有一个阴影无法去除,由于背景是白色的,这个阴影就特别显眼,非常不好看。接下来,将详细介绍一下遇到的问题。

二、自定义 ContentDilaog 样式

    笔者在使用UWP开发,需要自定义ContentDialog 的样式,自定义样式的方法其实很简单,网上也很多介绍,主要是自定义Style样式。首先,需要从 Windows Kits 安装目录中找到 ContentDilaog 的默认样式,默认样式文件在 [你电脑中WIndows Kits 安装目录]\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.18362.0\Generic\generic.xaml,找到这个文件并打开,搜索TargetType="ContentDialog" 就可以找到了,将整个Style复制出来,放到自己的项目中(可以新建自己的xaml资源文件,也可以直接放在 ContentDialog xaml声明文件中)。

    说到这里,必须注意的是,如果需要自定义 ContentDialog,你必须添加一个 xaml 文件来定义ContentDialog,在项目中右键 -> 添加 -> 新建项,在弹出的对话框中,选择C#,然后选“内容对话框”,输入对话框名称并“确定”,如下图:
创建内容对话框
    创建xaml文件后,内容大致如下:

<ContentDialog
    x:Class="Game.PrivacyPolicyDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Game"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    PrimaryButtonText="不同意"
    PrimaryButtonClick="PrivacyPolicyDialog_DenyButtonClick"
    SecondaryButtonText="同意并继续"
    SecondaryButtonClick="PrivacyPolicyDialog_AcceptButtonClick">
    <!-- 省略其他内容=-->
</ContentDialog>

说明:以上示例代码中,x:Class 对应的是cs文件类名称,如果你需要更改命名空间,要同时修改 xaml和cs文件,否则编译会报错。

    上面创建的内容对话框,样式是默认的样式,通过定义自定义样式,就可以改变原来的样式。如下示例:

ContentDialog
    x:Class="Game.PrivacyPolicyDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Game"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    PrimaryButtonText="不同意"
    PrimaryButtonClick="PrivacyPolicyDialog_DenyButtonClick"
    SecondaryButtonText="同意并继续"
    SecondaryButtonClick="PrivacyPolicyDialog_AcceptButtonClick">
    <ContentDialog.Resources>
        <ImageBrush x:Key="PrivacyPolicyDenyButtonBg" ImageSource="Assets/PrivacyPolicyDenyButtonBg.png" />
        <ImageBrush x:Key="PrivacyPolicyAcceptButtonBg" ImageSource="Assets/PrivacyPolicyAcceptButtonBg.png" />
        
        <!-- 隐私协议拒绝按钮样板 -->
        <Style TargetType="Button" x:Key="PrivacyPolicyDenyButtonStyle">
            <Setter Property="Background" Value="{StaticResource PrivacyPolicyDenyButtonBg}" />
            <Setter Property="Foreground" Value="#FFE30416" />
            <Setter Property="BorderBrush" Value="Transparent" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="Padding" Value="{ThemeResource ButtonPadding}" />
            <Setter Property="HorizontalAlignment" Value="Left" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
            <Setter Property="FontWeight" Value="Normal" />
            <Setter Property="FontSize" Value="30" />
            <Setter Property="UseSystemFocusVisuals" Value="{StaticResource UseSystemFocusVisuals}" />
            <Setter Property="FocusVisualMargin" Value="-3" />
            <Setter Property="Width" Value="302" />
            <Setter Property="Height" Value="80" />
            <Setter Property="HorizontalContentAlignment" Value="Center" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <ContentPresenter x:Name="ContentPresenter"
                                          Background="{TemplateBinding Background}"
                                          BackgroundSizing="{TemplateBinding BackgroundSizing}"
                                          BorderBrush="Transparent"
                                          BorderThickness="{TemplateBinding BorderThickness}"
                                          Content="{TemplateBinding Content}"
                                          ContentTemplate="{TemplateBinding ContentTemplate}"
                                          ContentTransitions="{TemplateBinding ContentTransitions}"
                                          CornerRadius="{TemplateBinding CornerRadius}"
                                          Padding="{TemplateBinding Padding}"
                                          HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                                          AutomationProperties.AccessibilityView="Raw">

                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal" >
                                        <Storyboard>
                                            <PointerUpThemeAnimation Storyboard.TargetName="ContentPresenter" />
                                        </Storyboard>
                                    </VisualState>

                                    <VisualState x:Name="PointerOver">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Background">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PrivacyPolicyDenyButtonBg}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="#FFE30416" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>

                                    <VisualState x:Name="Pressed">

                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Background">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PrivacyPolicyDenyButtonBg}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="#FFE30416" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>

                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Background">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PrivacyPolicyDenyButtonBg}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Gray" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>

                                </VisualStateGroup>

                            </VisualStateManager.VisualStateGroups>
                        </ContentPresenter>

                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <!-- 隐私协议接受按钮样板 -->
        <Style TargetType="Button" x:Key="PrivacyPolicyAcceptButtonStyle">
            <Setter Property="Background" Value="{StaticResource PrivacyPolicyAcceptButtonBg}" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="BorderBrush" Value="Transparent" />
            <Setter Property="BorderThickness" Value="{ThemeResource ButtonRevealBorderThemeThickness}" />
            <Setter Property="Padding" Value="{ThemeResource ButtonPadding}" />
            <Setter Property="HorizontalAlignment" Value="Left" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
            <Setter Property="FontWeight" Value="Normal" />
            <Setter Property="FontSize" Value="30" />
            <Setter Property="UseSystemFocusVisuals" Value="{StaticResource UseSystemFocusVisuals}" />
            <Setter Property="FocusVisualMargin" Value="-3" />
            <Setter Property="Width" Value="302" />
            <Setter Property="Height" Value="80" />
            <Setter Property="HorizontalContentAlignment" Value="Center" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <ContentPresenter x:Name="ContentPresenter"
                                  Background="{TemplateBinding Background}"
                                  BackgroundSizing="{TemplateBinding BackgroundSizing}"
                                  BorderBrush="{TemplateBinding BorderBrush}"
                                  BorderThickness="{TemplateBinding BorderThickness}"
                                  Content="{TemplateBinding Content}"
                                  ContentTemplate="{TemplateBinding ContentTemplate}"
                                  ContentTransitions="{TemplateBinding ContentTransitions}"
                                  CornerRadius="{TemplateBinding CornerRadius}"
                                  Padding="{TemplateBinding Padding}"
                                  HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                  VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                                  AutomationProperties.AccessibilityView="Raw">

                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal" >
                                        <Storyboard>
                                            <PointerUpThemeAnimation Storyboard.TargetName="ContentPresenter" />
                                        </Storyboard>

                                    </VisualState>

                                    <VisualState x:Name="PointerOver">

                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Background">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PrivacyPolicyAcceptButtonBg}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="White" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>

                                    <VisualState x:Name="Pressed">

                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Background">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PrivacyPolicyAcceptButtonBg}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="White" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>

                                    <VisualState x:Name="Disabled">

                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Background">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PrivacyPolicyAcceptButtonBg}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Gray" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>

                                </VisualStateGroup>

                            </VisualStateManager.VisualStateGroups>
                        </ContentPresenter>

                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <!-- 隐私协议对话框样式 -->
        <Style TargetType="local:PrivacyPolicyDialog">
            <Setter Property="Foreground" Value="{ThemeResource ContentDialogForeground}" />
            <Setter Property="Background">
                <Setter.Value>
                    <ImageBrush ImageSource="Assets/DialogBg.png"/>
                </Setter.Value>
            </Setter>
            <Setter Property="BackgroundSizing" Value="0" />
            <Setter Property="BorderBrush" Value="#00000000" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="IsTabStop" Value="False" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="local:PrivacyPolicyDialog">
                        <Border x:Name="Container" Background="#22000000">

                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="DialogShowingStates">

                                    <VisualStateGroup.Transitions>
                                        <VisualTransition To="DialogHidden">

                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="Visibility">
                                                    <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="Visible" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="IsHitTestVisible">
                                                    <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="False" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <DoubleAnimationUsingKeyFrames Storyboard.TargetName="ScaleTransform" Storyboard.TargetProperty="ScaleX">
                                                    <DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="1.0" />
                                                    <SplineDoubleKeyFrame KeyTime="0:0:0.5" KeySpline="0.1,0.9 0.2,1.0" Value="1.05" />
                                                </DoubleAnimationUsingKeyFrames>
                                                <DoubleAnimationUsingKeyFrames Storyboard.TargetName="ScaleTransform" Storyboard.TargetProperty="ScaleY">
                                                    <DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="1.0" />
                                                    <SplineDoubleKeyFrame KeyTime="0:0:0.5" KeySpline="0.1,0.9 0.2,1.0" Value="1.05" />
                                                </DoubleAnimationUsingKeyFrames>
                                                <DoubleAnimationUsingKeyFrames Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="Opacity">
                                                    <DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="1.0" />
                                                    <LinearDoubleKeyFrame KeyTime="0:0:0.083" Value="0.0" />
                                                </DoubleAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualTransition>
                                        <VisualTransition To="DialogShowing">

                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="Visibility">
                                                    <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="Visible" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <DoubleAnimationUsingKeyFrames Storyboard.TargetName="ScaleTransform" Storyboard.TargetProperty="ScaleX">
                                                    <DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="1.05" />
                                                    <SplineDoubleKeyFrame KeyTime="0:0:0.5" KeySpline="0.1,0.9 0.2,1.0" Value="1.0" />
                                                </DoubleAnimationUsingKeyFrames>
                                                <DoubleAnimationUsingKeyFrames Storyboard.TargetName="ScaleTransform" Storyboard.TargetProperty="ScaleY">
                                                    <DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="1.05" />
                                                    <SplineDoubleKeyFrame KeyTime="0:0:0.5" KeySpline="0.1,0.9 0.2,1.0" Value="1.0" />
                                                </DoubleAnimationUsingKeyFrames>
                                                <DoubleAnimationUsingKeyFrames Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="Opacity">
                                                    <DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="0.0" />
                                                    <LinearDoubleKeyFrame KeyTime="0:0:0.167" Value="1.0" />
                                                </DoubleAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualTransition>
                                    </VisualStateGroup.Transitions>
                                    <VisualState x:Name="DialogHidden" />
                                    <VisualState x:Name="DialogShowing">
                                        <VisualState.Setters>
                                            <Setter Target="LayoutRoot.Visibility" Value="Visible" />
                                            <Setter Target="BackgroundElement.TabFocusNavigation" Value="Cycle" />
                                        </VisualState.Setters>
                                    </VisualState>
                                    <VisualState x:Name="DialogShowingWithoutSmokeLayer">
                                        <VisualState.Setters>
                                            <Setter Target="LayoutRoot.Visibility" Value="Visible" />
                                            <Setter Target="LayoutRoot.Background" Value="{x:Null}" />
                                        </VisualState.Setters>
                                    </VisualState>

                                </VisualStateGroup>
                                <VisualStateGroup x:Name="DialogSizingStates">
                                    <VisualState x:Name="DefaultDialogSizing" />
                                    <VisualState x:Name="FullDialogSizing">
                                        <VisualState.Setters>
                                            <Setter Target="BackgroundElement.VerticalAlignment" Value="Stretch" />
                                        </VisualState.Setters>
                                    </VisualState>

                                </VisualStateGroup>
                                <VisualStateGroup x:Name="ButtonsVisibilityStates">
                                    <VisualState x:Name="AllVisible" />
                                    <VisualState x:Name="NoneVisible">
                                        <VisualState.Setters>
                                            <Setter Target="CommandSpace.Visibility" Value="Collapsed" />
                                        </VisualState.Setters>
                                    </VisualState>
                                    <VisualState x:Name="PrimaryVisible">
                                        <VisualState.Setters>
                                            <Setter Target="PrimaryButton.(Grid.Column)" Value="2" />
                                            <Setter Target="PrimaryButton.(Grid.ColumnSpan)" Value="2" />
                                            <Setter Target="PrimaryButton.Margin" Value="2,0,0,0" />
                                            <Setter Target="SecondaryButton.Visibility" Value="Collapsed" />
                                            <Setter Target="CloseButton.Visibility" Value="Collapsed" />
                                        </VisualState.Setters>
                                    </VisualState>
                                    <VisualState x:Name="SecondaryVisible">
                                        <VisualState.Setters>
                                            <Setter Target="SecondaryButton.(Grid.Column)" Value="2" />
                                            <Setter Target="SecondaryButton.(Grid.ColumnSpan)" Value="2" />
                                            <Setter Target="SecondaryButton.Margin" Value="2,0,0,0" />
                                            <Setter Target="PrimaryButton.Visibility" Value="Collapsed" />
                                            <Setter Target="CloseButton.Visibility" Value="Collapsed" />
                                        </VisualState.Setters>
                                    </VisualState>
                                    <VisualState x:Name="CloseVisible">
                                        <VisualState.Setters>
                                            <Setter Target="CloseButton.(Grid.Column)" Value="2" />
                                            <Setter Target="CloseButton.(Grid.ColumnSpan)" Value="2" />
                                            <Setter Target="CloseButton.Margin" Value="2,0,0,0" />
                                            <Setter Target="PrimaryButton.Visibility" Value="Collapsed" />
                                            <Setter Target="SecondaryButton.Visibility" Value="Collapsed" />
                                        </VisualState.Setters>
                                    </VisualState>
                                    <VisualState x:Name="PrimaryAndSecondaryVisible">
                                        <VisualState.Setters>
                                            <Setter Target="PrimaryButton.(Grid.ColumnSpan)" Value="2" />
                                            <Setter Target="SecondaryButton.(Grid.Column)" Value="2" />
                                            <Setter Target="SecondaryButton.(Grid.ColumnSpan)" Value="2" />
                                            <Setter Target="SecondaryButton.Margin" Value="2,0,0,0" />
                                            <Setter Target="CloseButton.Visibility" Value="Collapsed" />
                                        </VisualState.Setters>
                                    </VisualState>
                                    <VisualState x:Name="PrimaryAndCloseVisible">
                                        <VisualState.Setters>
                                            <Setter Target="PrimaryButton.(Grid.ColumnSpan)" Value="2" />
                                            <Setter Target="CloseButton.(Grid.Column)" Value="2" />
                                            <Setter Target="CloseButton.(Grid.ColumnSpan)" Value="2" />
                                            <Setter Target="CloseButton.Margin" Value="2,0,0,0" />
                                            <Setter Target="SecondaryButton.Visibility" Value="Collapsed" />
                                        </VisualState.Setters>
                                    </VisualState>
                                    <VisualState x:Name="SecondaryAndCloseVisible">
                                        <VisualState.Setters>
                                            <Setter Target="SecondaryButton.(Grid.Column)" Value="0" />
                                            <Setter Target="SecondaryButton.(Grid.ColumnSpan)" Value="2" />
                                            <Setter Target="SecondaryButton.Margin" Value="0,0,2,0" />
                                            <Setter Target="CloseButton.(Grid.Column)" Value="2" />
                                            <Setter Target="CloseButton.(Grid.ColumnSpan)" Value="2" />
                                            <Setter Target="CloseButton.Margin" Value="2,0,0,0" />
                                            <Setter Target="PrimaryButton.Visibility" Value="Collapsed" />
                                        </VisualState.Setters>
                                    </VisualState>

                                </VisualStateGroup>
                                <VisualStateGroup x:Name="DefaultButtonStates">
                                    <VisualState x:Name="NoDefaultButton" />
                                    <VisualState x:Name="PrimaryAsDefaultButton">
                                        <VisualState.Setters>
                                            <Setter Target="PrimaryButton.Style" Value="{StaticResource AccentButtonStyle}" />
                                        </VisualState.Setters>
                                    </VisualState>
                                    <VisualState x:Name="SecondaryAsDefaultButton">
                                        <VisualState.Setters>
                                            <Setter Target="SecondaryButton.Style" Value="{StaticResource AccentButtonStyle}" />
                                        </VisualState.Setters>
                                    </VisualState>
                                    <VisualState x:Name="CloseAsDefaultButton">
                                        <VisualState.Setters>
                                            <Setter Target="CloseButton.Style" Value="{StaticResource AccentButtonStyle}" />
                                        </VisualState.Setters>
                                    </VisualState>

                                </VisualStateGroup>
                                <VisualStateGroup x:Name="DialogBorderStates">
                                    <VisualState x:Name="NoBorder" />
                                    <VisualState x:Name="AccentColorBorder">
                                        <VisualState.Setters>
                                            <Setter Target="BackgroundElement.BorderBrush" Value="{ThemeResource SystemControlForegroundAccentBrush}" />
                                        </VisualState.Setters>
                                    </VisualState>

                                </VisualStateGroup>

                            </VisualStateManager.VisualStateGroups>
                            <Grid x:Name="LayoutRoot" Visibility="Collapsed" Background="{ThemeResource SystemControlPageBackgroundMediumAltMediumBrush}">
                                <Border x:Name="BackgroundElement"
                                        Background="{TemplateBinding Background}"
                                        FlowDirection="{TemplateBinding FlowDirection}"
                                        BorderThickness="1"
                                        BorderBrush="#08000000"
                                        CornerRadius="18"
                                        MinWidth="858"
                                        MaxWidth="858"
                                        MinHeight="617"
                                        MaxHeight="617"
                                        HorizontalAlignment="Center"
                                        VerticalAlignment="Center"
                                        RenderTransformOrigin="0.5,0.5">
                                    <Border.RenderTransform>
                                        <ScaleTransform x:Name="ScaleTransform" />
                                    </Border.RenderTransform>
                                    <Grid x:Name="DialogSpace" Padding="15,15,15,15" CornerRadius="15">
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="*" />
                                            <RowDefinition Height="Auto" />
                                        </Grid.RowDefinitions>
                                        <ScrollViewer x:Name="ContentScrollViewer"
                                              HorizontalScrollBarVisibility="Disabled"
                                              VerticalScrollBarVisibility="Disabled"
                                              HorizontalScrollMode="Disabled"
                                              VerticalScrollMode="Disabled"
                                              ZoomMode="Disabled"
                                              Margin="{ThemeResource ContentDialogContentScrollViewerMargin}"
                                              IsTabStop="False">
                                            <Grid>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="Auto" />
                                                    <RowDefinition Height="*" />
                                                </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">
                                                    <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"
                                              XYFocusKeyboardNavigation="Enabled"
                                              Padding="35,25,35,35"
                                              Margin="{ThemeResource ContentDialogCommandSpaceMargin}">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition />
                                                <ColumnDefinition Width="0.5*" />
                                                <ColumnDefinition Width="0.5*" />
                                                <ColumnDefinition />
                                            </Grid.ColumnDefinitions>
                                            <Button x:Name="PrimaryButton"
                                                    Content="{TemplateBinding PrimaryButtonText}"
                                                    IsEnabled="{TemplateBinding IsPrimaryButtonEnabled}"
                                                    Style="{StaticResource PrivacyPolicyDenyButtonStyle}"
                                                    HorizontalAlignment="Left"
                                                    VerticalAlignment="Stretch"
                                                    Margin="0,0,2,0"
                                                    Grid.Column="0" />
                                            <Button x:Name="SecondaryButton"
                                                    Content="{TemplateBinding SecondaryButtonText}"
                                                    IsEnabled="{TemplateBinding IsSecondaryButtonEnabled}"
                                                    Style="{StaticResource PrivacyPolicyAcceptButtonStyle}"
                                                    ElementSoundMode="FocusOnly"
                                                    HorizontalAlignment="Right"
                                                    VerticalAlignment="Stretch"
                                                    Margin="2,0,2,0"
                                                    Grid.Column="1"
                                                    Grid.ColumnSpan="2" />
                                            <Button x:Name="CloseButton"
                                                    Content="{TemplateBinding CloseButtonText}"
                                                    Style="{TemplateBinding CloseButtonStyle}"
                                                    ElementSoundMode="FocusOnly"
                                                    HorizontalAlignment="Stretch"
                                                    VerticalAlignment="Stretch"
                                                    Visibility="Collapsed"
                                                    Margin="2,0,0,0"
                                                    Grid.Column="3" />
                                        </Grid>

                                    </Grid>
                                </Border>

                            </Grid>
                        </Border>

                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ContentDialog.Resources>

    <Grid HorizontalAlignment="Center" Width="Auto">
        <Grid.RowDefinitions>
            <RowDefinition Height="120" />
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="750" />
        </Grid.ColumnDefinitions>

        <TextBlock FontSize="36" Foreground="Black" FontWeight="Bold"
                   HorizontalAlignment="Center" VerticalAlignment="Center">用户隐私政策提示</TextBlock>

        
        <WebView x:Name="PrivacyPolicyWebView" Grid.Row="1" 
                 ScrollViewer.HorizontalScrollMode="Disabled"
                 ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                 ScrollViewer.VerticalScrollMode="Enabled"
                 ScrollViewer.VerticalScrollBarVisibility="Hidden"/>

    </Grid>
</ContentDialog>

说明:样式文件中,TargetType="local:PrivacyPolicyDialog" 表示将次样式文件应用到指定的内容对话框类中,如果需要多个内容对话框类使用相同的样式,可以使用资源文件的方式定义样式,并声明 x:Key 值,在内容对话框的 Style 属性中指定。

三、问题描述

    上面的自定义样式之后,通过修改默认样板,也就是Template属性,就可以更改默认的布局,自定义样式后,效果如下图:
有阴影的效果
    从上图可以看到,因为自定义背景白色且有圆角,阴影就显得尤其难看。在自定义的样式文件中修改了所有的属性,都无法去除,就算将对话框内容部分的背景改为透明,仍旧有一个半透明黑色的框框,如下图:
去不掉的半透明黑色框

四、解决方案

    其实上图的半透明黑色框,是阴影。通过网上查阅相关资料,终于找到最完美的解决方案,就是通过修改 ContentDialogTranslation 属性,将 Y 坐标修改得足够低,将不会显示阴影。

<ContentDialog
    x:Class="GamePrivacyPolicyDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Game"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Translation="0,0,-100"
    PrimaryButtonText="不同意"
    PrimaryButtonClick="PrivacyPolicyDialog_DenyButtonClick"
    SecondaryButtonText="同意并继续"
    SecondaryButtonClick="PrivacyPolicyDialog_AcceptButtonClick">
    <!-- 这里省略了其他内容 -->
</ContentDialog>

笔者将Z坐标改为了-100(Translation="0,0,-100"),这样阴影完全消失,如果不放心,可以改得更低的值,比如-1000甚至更低。

    通过修改Translation 属性的 Z 坐标,阴影可以完全消失,如下图:
去掉阴影的效果

六、编后语

    内容对话框这个阴影问题,确实比较头疼,笔者也是找了很久才找到解决办法。网上也有说可以通过 Shadow 属性配置,但是这个属性需要较高的目标版本,如果其他开发者使用较高目标版本,可以验证一下。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/656821.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

数据库多表查询

多表查询&#xff1a; SELECT *FROM stu_table,class WHERE stu_table.c_idclass.c_id; 多表查询——内连接 查询两张表交集部分。 隐式内连接&#xff1a; #查询学生姓名&#xff0c;和班级名称&#xff0c;隐式调用 SELECT stu_table.s_name,class.c_name FROM stu_table…

php反序列化学习(1)

1、php面向对象基本概念 类的定义&#xff1a; 类是定义了一件事物的抽象特征&#xff0c;它将数据的形式以及这些数据上的操作封装住在一起。&#xff08;对象是具有类类型的变量&#xff0c;是对类的实例&#xff09; 构成&#xff1a; 成员变量&#xff08;属性&#xf…

来自工业界的知识库 RAG 服务(二),RagFlow 源码全流程深度解析

背景介绍 前面介绍过 有道 QAnything 源码解析&#xff0c;通过深入了解工业界的知识库 RAG 服务&#xff0c;得到了不少调优 RAG 服务的新想法。 因此本次趁热打铁&#xff0c;额外花费一点时间&#xff0c;深入研究了另一个火热的开源 RAG 服务 RagFlow 的完整实现流程&…

上交提出TrustGAIN,提出6G网络中可信AIGC新模式!

月16日至18日&#xff0c;2024全球6G技术大会在南京召开。会上&#xff0c;全球移动通信标准制定组织3GPP&#xff08;第三代合作伙伴计划&#xff09;的3位联席主席分享了3GPP6G标准时间表&#xff1a; 2024年9月&#xff0c;启动6G业务需求研究&#xff1b; 2025年6月&…

FastReport 主子表关系

代码中只需要绑定主表的数据就可以&#xff0c;子表的数据会通过报表中的关连关系自动到数据库中带出。 using CloudSaaS.DB.Handler; using CloudSaaS.Model; using CloudSaaS.DAL; using FastReport; using FastReport.Web; using System; using System.Collections.Generic;…

Hotcoin Research | 市场洞察:2024年5月13日-5月19日

加密货币市场表现 目前&#xff0c;加密货币总市值为1.32万亿&#xff0c;BTC占比54.41%。 本周行情呈现震荡上行的态势&#xff0c;BTC在5月15日-16日&#xff0c;有一波大的拉升&#xff0c;周末为震荡行情。BTC现价为67125美元。 上涨的主要原因&#xff1a;美国4月CPI为3…

Oracle创建用户时提示ORA-65096:公用用户名或角色名无效

Oracle创建用户时提示“ORA-65096&#xff1a;公用用户名或角色名无效” 如下图所示&#xff1a; 解决方法&#xff1a;在新增用户名前面加上C##或者c##就可以解决无效问题&#xff0c;具体什么原因还不清楚&#xff0c;需要再研究一下。

JS 中怎么删除数组元素?有哪几种方法?

正文开始之前推荐一位宝藏博主免费分享的学习教程,学起来! 编号学习链接1Cesium: 保姆级教程+源码示例2openlayers: 保姆级教程+源码示例3Leaflet: 保姆级教程+源码示例4MapboxGL: 保姆级教程+源码示例splice() JavaScript中的splice()方法是一个内置的数组对象函数, 用于…

vr数字成果展在线展示突破用户传统认知

想要轻松搭建一个充满互动与创意的3D数字展厅吗?vr互动数字展厅搭建编辑器将是您的不二之选!华锐视点3D云展平台提供的vr互动数字展厅搭建编辑器将空间重建与互动制作完美结合&#xff0c;让您轻松实现3D空间的搭建与互动营销制作。 在vr互动数字展厅搭建编辑器的帮助下&#…

SpringBoot 返回值 i18n 自动处理

定义基础通用类 首先定义一波错误码&#xff1a;ResultCode Getter AllArgsConstructor public enum ResultCode {SUCCESS(200, "请求成功", "request.success"),Fail(400, "请求失败", "request.failed"),PASSWORD_NOT_MATCH(1000…

独家揭秘!Amazon、lazada、Shopee测评自养号,新手也能秒变高手!

近年来&#xff0c;随着国内卖家涌入跨境电商平台&#xff0c;市场竞争愈加激烈。为了迅速占领市场&#xff0c;测评变得至关重要。然而&#xff0c;真人测评供不应求&#xff0c;服务商账号质量不一&#xff0c;且存在高权重账号稀缺和黑卡下单风险。因此&#xff0c;许多大卖…

为什么选择CleanMyMac软件呢?推荐理由

你是否曾经遇到过这样的问题&#xff1a;电脑运行缓慢&#xff0c;存储空间不足&#xff0c;不知道如何清理垃圾文件&#xff1f;别担心&#xff0c;我们为你找到了解决方案——CleanMyMac软件。这款强大的工具可以帮助你轻松解决这些问题&#xff0c;让你的电脑焕然一新&#…

VirtualBox+Ubuntu22.10+Docker+ROS2

Docker 拉取ros2镜像 docker pull osrf/ros:foxy-desktop 运行 docker run -it --nameros2 -p 50022:22 osrf/ros:foxy-desktop 进入容器安装组件 apt-get update apt-get install vim apt-get install git apt-get install net-tools # 安装ssh apt-get install openssh…

【FPGA】正原子XC7A35T

25_实战篇&#xff1a;时钟IP核MMCM&#xff08;第一讲&#xff1a;时钟资源讲解&#xff09;_哔哩哔哩_bilibili 25时钟IP核MMCM 7系列的时钟资源 bufferG bufferR 下图可视为一个FPGA&#xff08;官方手册&#xff09; 4 MRCC,SRCC 全局时钟&#xff1a;MRCC P 差分时…

Java入门-“第九大数据类型“-字符串

字符串String **字符串(String)**是指多个字符连接起来组合成的字符序列&#xff0c;例如”中国”&#xff0c;“hello world”都为字符串。注意对比字符&#xff0c;字符只能存储一个字符使用单引号’中’&#xff0c;’国’。 字符串底层源码 字符串定义 创建String对象 St…

2024年5月软考成绩什么时候出?附查询方式

2024年5月软考成绩查询时间及查询方式&#xff1a; 查询时间&#xff1a;预计在2024年7月上旬进行。 查询方式&#xff1a; 方式一&#xff1a;登陆中国计算机技术职业资格网&#xff08;www.ruankao.org.cn&#xff09;&#xff0c;点击报名系统&#xff0c;输入注册账号和…

nodejs中使用ffmpeg零基础教程(electron+vue3)

同学们可以私信我加入学习群&#xff01; 正文开始 前言一、多方案对比二、ffmpeg各插件简介三、使用ffmpeg-static插件四、使用fluent-ffmpeg插件五、如果使用ai&#xff0c;可能会踩的坑5.1第一个坑5.2第二个坑5.3第三个坑 总结 前言 最近想要把自己写的一些知识点&#xff…

【NumPy】全面解析NumPy的astype函数:高效数据类型转换指南

&#x1f9d1; 博主简介&#xff1a;阿里巴巴嵌入式技术专家&#xff0c;深耕嵌入式人工智能领域&#xff0c;具备多年的嵌入式硬件产品研发管理经验。 &#x1f4d2; 博客介绍&#xff1a;分享嵌入式开发领域的相关知识、经验、思考和感悟&#xff0c;欢迎关注。提供嵌入式方向…

VUE-watch和watchEffect的区别

区别简短扼要地说&#xff1a; watch-官方定义&#xff1a;侦听一个或多个响应式数据源&#xff0c;并在数据源变化时调用所给的回调函数。是需要指定监听的数据&#xff0c;并且只有在响应式数据变化的时候去执行 watchEffect-官方定义&#xff1a;立即运行一个函数&#xff0…

mybatis关联查询使用resultMap查询到了多条,结果返回一条。

今天在写代码时候&#xff0c;遇到了一个很让我费解的问题&#xff0c;在使用关联查询的时候&#xff0c;在明明数据库里面&#xff0c;已经查到了两条数据&#xff0c;结果resultMap这个集合里面&#xff0c;就只返回一条数据。 数据库的SQL&#xff1a; mybatis的xml里面的r…