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.rhkb.cn/news/334606.html

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

相关文章

18 - grace数据处理 - 补充 - 地下水储量计算过程分解 - 地表水储量变化Glads水文数据处理

18 - grace数据处理 - 补充 - 地下水储量计算过程分解 - 地表水储量变化 0 引言1 Grace陆地水储量过程整合0 引言 由水量平衡方程可以将地下水储量的计算过程分解为3个部分,第一部分计算陆地水储量变化、第二部分计算地表水储量变化、第三部分计算地下水储量变化。本篇简单介绍…

Android 逆向学习【1】——版本/体系结构/代码学习

#Android 历史版本 参考链接&#xff1a;一篇文章让你了解Android各个版本的历程 - 知乎 (zhihu.com) 三个部分&#xff1a;api等级、版本号、代号&#xff08;这三个东西都是指的同一个系统&#xff09; API等级&#xff1a;在APP开发的时候写在清单列表里面的 版本号&…

【Python-OS】os.path.splitext()

作用&#xff1a;将文件路径分割成文件名和扩展名两部分。 slide_id, _ os.path.splitext(slide) print("slide:") print(slide) print("slide_id:") print(slide_id)注&#xff1a; slide是文件名&#xff0c;可以自行赋值

【Go语言入门学习笔记】Part3.指针和运算符、以及基本输入

一、前言 仍然好多和C语言类似&#xff0c;计算机的学生应该是很容易入门这一环节&#xff0c;我还在最后的输入中看到了一些些Java输入的影子&#xff0c;而自动的变量类型推断更是有Python那个味道&#xff0c;正可谓几百家之所长了。 二、学习代码 package mainimport (&q…

Angular(1):使用Angular CLI创建空项目

要创建一个空的 Angular 项目&#xff0c;可以使用 Angular CLI&#xff08;命令行界面&#xff09;。以下是使用 Angular CLI 创建一个新项目的步骤&#xff1a; 1、安装 Angular CLI&#xff1a; 打开你的命令行界面&#xff08;在 Windows 上是 CMD、PowerShell 或 Git Bas…

动态内存管理—C语言通讯录

目录 一&#xff0c;动态内存函数的介绍 1.1 malloc和free 1.2 calloc 1.3 realloc 1.4C/C程序的内存开辟 二&#xff0c;通讯录管理系统 动态内存函数的介绍 malloc free calloc realloc 一&#xff0c;动态内存函数的介绍 1.1 malloc和free void* malloc (…

mysql实战——Mysql8.0高可用之双主+keepalived

一、介绍 利用keepalived实现Mysql数据库的高可用&#xff0c;KeepalivedMysql双主来实现MYSQL-HA&#xff0c;两台Mysql数据库的数据保持完全一致&#xff0c;实现方法是两台Mysql互为主从关系&#xff0c;通过keepalived配置VIP&#xff0c;实现当其中的一台Mysql数据库宕机…

Vite + Vue3 + Electron 创建打包桌面程序

10 【Vite Vue3 Electron 创建打包桌面程序】 1.使用 Vite 构建 Electron 项目 1.1 创建 Vite 应用&#xff0c;安装 Electron 依赖 创建一个 Vite 项目 npm init vitelatest安装 Electron 相关依赖 npm install electron -D npm install vite-plugin-electron -D 1.2 在…

拉格朗日插值及牛顿差商方法的实现(Matlab)

一、问题描述 拉格朗日插值及牛顿差商方法的实现。 二、实验目的 掌握拉格朗日插值和牛顿差商方法的原理&#xff0c;能够编写代码实现两种方法&#xff1b;能够分析多项式插值中的误差。 三、实验内容及要求 利用拉格朗日插值及牛顿差商方法估计1980 年的人口&#xff0c;并…

FreeRTOS_信号量_学习笔记

信号量的特性 消息队列用于传输多个数据&#xff0c;但是有时候我们只需要传递状态&#xff0c;这个状态值需要用一个数值表示。套用队列笔记中的流水线例子&#xff0c;可以理解为流水线上工件的数量。 信号&#xff1a;起通知作用 量&#xff1a;还可以用来表示资源的数量 当…

场景文本检测识别学习 day10(MMdetection)

配置文件(config) 由于在大型项目中&#xff0c;一种模型需要分&#xff1a;tiny、small、big等很多种&#xff0c;而它们的区别主要在网络结构&#xff0c;数据的加载&#xff0c;训练策略等&#xff0c;且差别很多都很小&#xff0c;所以如果每个模型都手动从头写一份&#…

三十、openlayers官网示例解析Double click, Drag and Zoom——第二次点击鼠标拖拽缩放地图效果、取消地图双击放大事件

这篇展示了如何在地图上添加第二次按下鼠标移动鼠标实现拖拽缩放地图效果。 官网demo地址&#xff1a; Double click, Drag and Zoom 官网介绍文字的翻译如下&#xff1a; 示例比较简单&#xff0c;直接贴代码&#xff1a; const map new Map({//添加第二次点击拖拽缩放地图i…

【Postman接口测试】第二节.Postman界面功能介绍(上)

文章目录 前言一、Postman前言介绍二、Postman界面导航说明三、使用Postman发送第一个请求四、Postman 基础功能介绍 4.1 常见类型的接口请求 4.1.1 查询参数的接口请求 4.1.2 表单类型的接口请求 4.1.3 上传文件的表单请求 4.1.4 JSON 类…

vue 展示svg矢量图可缩放拖动

使用插件&#xff1a;svg-pan-zoom <template> <!-- svg图--><div id"svgContainer"></div> </template><script> import svgPanZoom from svg-pan-zoom import svgFile from ../datav/img/220kVscb.svg // 路径根据实际情况调…

go-zero 实战(1)

环境准备 go 版本 go version go1.22.2 linux/amd64 goctl 安装 goctl&#xff08;官方建议读 go control&#xff09;是 go-zero微服务框架下的代码生成工具。使用 goctl 可以显著提升开发效率&#xff0c;让开发人员将时间重点放在业务开发上&#xff0c;其功能有&#xff1a…

关于我转生从零开始学C++这件事:升级Lv.25

❀❀❀ 文章由不准备秃的大伟原创 ❀❀❀ ♪♪♪ 若有转载&#xff0c;请联系博主哦~ ♪♪♪ ❤❤❤ 致力学好编程的宝藏博主&#xff0c;代码兴国&#xff01;❤❤❤ OK了老铁们&#xff0c;又是一个周末&#xff0c;大伟又来继续给大家更新我们的C的内容了。那么根据上一篇博…

uniapp中使用mockjs模拟接口测试总结(swiper轮播图示例)

完整总结下在uni-app中如何使用Mock.js模拟接口测试&#xff0c;这在后台接口未就绪的情况下非常有用。同时也给出个首页swiper轮播图的mock接口使用。网上的文章都不太完整&#xff0c;这里总结下完整的使用示例&#xff0c;同时也支持h5和小程序平台&#xff0c;分享给需要的…

OrangePi AIpro初体验:开启嵌入式开发之旅

概述 随着物联网和智能设备时代的到来&#xff0c;单板电脑因其独特的优势成为创新项目和教育实践的重要工具。在众多单板电脑中&#xff0c;香橙派以其出色的性能和亲民的价格&#xff0c;十分吸引博主这初涉嵌入式开发的新手。博主有幸被CSDN邀请对OrangePi AIpro进行测评。…

QT学习(20):QStyle和自定义样式

QStyle 样式&#xff08;继承自QStyle类&#xff09;代表控件的绘制并封装GUI的外观。QStyle是一个封装了GUI外观的抽象基类。Qt使用QStyle去执行几乎所有的内置控件的绘制&#xff0c;确保控件外观和原生控件风格风格相同。 class Q_WIDGETS_EXPORT QStyle : public QObject{…

区块链技术和应用二

前言 学习长安链的一些基本原理 官网&#xff1a;长安链开源文档 b站课程&#xff1a;区块链基础与应用 一、共识算法 1.1 POW工作量证明 最长链共识&#xff0c;没听明白 1.2 51%攻击 二、区块链的发展 2.1 区块链1.0到3.0 2.2 共有链、联盟链、私有链 2.3 发展趋势 2.4 扩…