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文件后,内容大致如下:

<ContentDialogx: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文件,否则编译会报错。

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

ContentDialogx: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 坐标修改得足够低,将不会显示阴影。

<ContentDialogx: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 属性配置,但是这个属性需要较高的目标版本,如果其他开发者使用较高目标版本,可以验证一下。

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

使用prometheus监测MySQL主从同步状态方案

说明&#xff1a;本文介绍如何使用prometheus、alertmanager监测MySQL主从&#xff0c;当从节点中断同步时&#xff0c;发送邮箱报警&#xff0c;并使用grafana将数据视图化。 结构图如下&#xff1a; 安装 &#xff08;1&#xff09;安装应用 首先&#xff0c;来安装promet…

【Linux】线程安全及锁的使用

文章目录 前言一、锁1.定义一个锁变量2.pthread_mutex_init3.pthread_mutex_destroy4.pthread_mutex_lock/pthread_mutex_unlock5.静态变量锁和全局变量锁的初始化 二、问题描述及锁的运用三、RAII风格的锁 前言 临界资源: 在多个线程或进程间共享的资源. 临界区: 代码中访问临…

5 分钟快速上手图形验证码,防止接口被恶意刷量!

5 分钟快速上手图形验证码&#xff0c;防止接口被恶意刷量&#xff01; 大家好&#xff0c;我是程序员小白条&#xff0c;今天来给大家介绍一个快速实现图形验证码的优秀框架 AJ-Captcha。 需求分析 如果注册接口没有验证码这种类型的限制&#xff0c;很容易会被刷量&#x…

protobuf —— 快速上手

protobuf —— 快速上手 创建 .proto 文件添加注释指定proto3语法package 声明符定义消息&#xff08;message&#xff09; 定义消息字段字段定义基本格式字段名称命名规范字段类型字段唯一编号示例 转换关系示例&#xff1a;增加姓名和年龄字段 字段唯一编号字段编号范围编码效…

英伟达SSD视觉算法,jetson.inference在jetson nano中部署

一、用官方镜像刷机 安装SD卡擦除工具SD Card Formatter https://www.sdcardformatter.com/download/ 格式化SD卡 下载官方镜像 https://developer.nvidia.com/jetson-nano-sd-card-image 安装刷机工具balenaEtcher https://www.balena.io/etcher 将上面下载的镜像压缩包解…

spark的简单学习二

一 spark sql基础 1.1 Dataframe 1.介绍&#xff1a; DataFrame也是一个分布式数据容器。然而DataFrame更像传统数据库的二维表 格&#xff0c;除了数据以外&#xff0c;还掌握数据的结构信息&#xff0c;即schema。同时&#xff0c;与Hive类似&#xff0c;DataFrame也支 持…

gin框架精通篇(二)

原生数据库使用 导入模块&#xff1a;go get -u github.com/go-sql-driver/mysql 安装 mysql 数据库 安装数据库可能遇到的问题&#xff1a;&#xff08;网上的方法基本可以解决&#xff09; ERROR 1045 (28000): Access denied for user ‘-root’‘localhost’ (using passwo…

HTML 页面布局

慢慢生活&#xff0c;慢慢变好 —— 24.5.28 页面布局 盒子: 页面中所有的元素(标签)&#xff0c;都可以看做是一个盒子&#xff0c;由盒子将页面中的元素包含在一个矩形区域内&#xff0c;通过盒子的视角更方便的进行页面布局 盒子模型组成: 内容区域(content)、内边距区域(pa…

数据结构的希尔排序(c语言版)

一.希尔排序的概念 1.希尔排序的基本思想 希尔排序是一种基于插入排序算法的优化排序方法。它的基本思想如下: 选择一个增量序列 t1&#xff0c;t2&#xff0c;......&#xff0c;tk&#xff0c;其中 ti > tj, 当 i < j&#xff0c;并且 tk 1。 按增量序列个数k&#…

Centos安装,window、ubuntus双系统基础上安装Centos安装

文章目录 前言一、准备工作二、开始安装1、2、首先选择DATE&TIME2、选择最小安装3、 选择安装位置 总结 前言 因工作需要&#xff0c;我需要在工控机上额外装Centos7系统&#xff0c;不过我是装在机械硬盘上了不知道对性能是否有影响&#xff0c;若有影响&#xff0c;后面…

基于C#开发web网页管理系统模板流程-主界面管理员入库和出库功能完善

前言 紧接上篇->基于C#开发web网页管理系统模板流程-主界面管理员录入和编辑功能完善-CSDN博客 本篇将完善主界面的管理员入库和出库功能&#xff0c;同样的&#xff0c;管理员入库和出库的设计套路适用于动态表的录入和编辑 首先还是介绍一下本项目将要实现的功能 &#xf…

[Android]项目打包APK时报错PKCS12 keystore not in version 3 format

报错&#xff1a; PKCS12 keystore not in version 3 format Execution failed for task :app:packageRelease. > A failure occurred while executing com.android.build.gradle.tasks.PackageAndroidArtifact$IncrementalSplitterRunnable > com.android.ide.commo…

如何为个人网站部署SSL安全证书,以实现网站的 HTTPS 加密协议访问?

哈喽&#xff0c;大家好呀&#xff01;这里是码农后端。完成了域名的备案与解析后&#xff0c;就可以通过域名来访问我们的网站了。本篇将介绍如何为我们的网站部署SSL安全证书&#xff0c;实现网站的 HTTPS 加密协议访问。 1、购买SSL证书 未进行SSL证书部署&#xff0c;访问网…

回答篇二:测试开发高频面试题目

引用之前文章&#xff1a;测试开发高频面试题目 本篇文章是回答篇&#xff08;持续更新中&#xff09; 1. 在测试开发中使用哪些自动化测试工具和框架&#xff1f;介绍一下你对其中一个工具或框架的经验。 a. 测试中经常是用的自动化测试工具和框架有Selenium、Pytest、Postman…

调整表格大小

方法一&#xff1a;使用鼠标拖动表格边框或右下角的调整控点 在Word文档中&#xff0c;选中要缩小的表格&#xff0c;将鼠标指针放在表格的边框线上&#xff0c;直到指针变成双箭头的形状。 按住鼠标左键&#xff0c;拖动边框线&#xff0c;调整表格的宽度或高度。如果同时按住…

AI视频教程下载:使用ChatGPT进行商务写作

你将学到什么&#xff1f; 学习如何将ChatGPT集成到你的写作过程中&#xff0c;并有效地将其用作商务写作的个人写作助手。 学习如何使用ChatGPT生成想法&#xff0c;提高你的书面沟通的结构、清晰度和连贯性。 你将学习使用ChatGPT的最佳实践&#xff0c;包括如何自定义其设…

Win10版本TDengine使用分享

软件介绍 TDengine是一款开源、高性能、可扩展的时间序列数据库&#xff08;TSDB&#xff09;。它由涛思数据公司开发&#xff0c;专为处理大规模时间序列数据而设计。时间序列数据是指按时间顺序排列的数据点序列&#xff0c;广泛应用于物联网、大数据分析、金融等领域。TDen…

Redis解决缓存一致性问题

文章目录 ☃️概述☃️数据库和缓存不一致采用什么方案☃️代码实现☃️其他 ☃️概述 由于我们的 缓存的数据源来自于数据库, 而数据库的 数据是会发生变化的, 因此,如果当数据库中 数据发生变化,而缓存却没有同步, 此时就会有 一致性问题存在, 其后果是: 用户使用缓存中的过…

DSP6657 GPIO中断学习

1 简介 使用创龙板卡的KEY2按键通过中断的方式控制LED3的亮灭 2 中断学习 在C665x设备上&#xff0c;CPU中断是通过C66x CorePac中断控制器进行配置的。该中断控制器允许最多128个系统事件被编程到任意12个CPU可屏蔽中断输入&#xff08;CPUINT4至CPUINT15&#xff09;、CPU…

短剧解说一键生成原创文案的快速方法

如今短剧创作火的一塌糊涂&#xff0c;它们以其简洁明了的剧情、生动有趣的角色和紧凑的节奏&#xff0c;吸引了大量观众的关注。因此&#xff0c;它所带来的流量是非常巨大&#xff0c;不少人将流量的获取瞄准了短剧创作领域以及短剧解说领域。而对于短剧解说人员来讲&#xf…