本文实例为大家分享了WPF实现3D翻牌式倒计时的具体代码,供大家参考,具体内容如下
实现效果如下:
思路:使用自定义控件,设置一个背板 MyCardControlBottom,一个卡牌翻动的前部 MyCardControlFront,一个卡牌翻动后的背部 MyCardControlBack,另外实现卡牌翻动的MyCardControl;在主窗体中设置一计时器,根据卡牌上的数字和计时器时间启动翻牌动作。
主要代码:
1、自定义控件MyCardControlBottom
<UserControl x:Class="TurnOverCards.MyCardControlBottom" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" x:Name="MyUserControl" Height="300" Width="200"> <Border BorderThickness="0"> <Border.Effect> <DropShadowEffect BlurRadius="20" Color="Gray" Direction="-90" ShadowDepth="10"></DropShadowEffect> </Border.Effect> <p>本文来源gao!%daima.com搞$代*!码9网(</p> <Border.Background> <RadialGradientBrush GradientOrigin="0.5,0.5" Center="0.5,0.5" RadiusX="0.75" RadiusY="0.65"> <GradientStop Color="DimGray" Offset="0" /> <GradientStop Color="Black" Offset="1" /> </RadialGradientBrush> </Border.Background> <Grid> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Text="{Binding ElementName=MyUserControl,Path=BottomText}" FontFamily="Arial" Foreground="White" FontSize="150" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,65,0,2"> <TextBlock.Effect> <DropShadowEffect BlurRadius="10" Color="Black" Direction="-90" ShadowDepth="2"></DropShadowEffect> </TextBlock.Effect> </TextBlock> <TextBlock Grid.Row="1" Text="{Binding ElementName=MyUserControl,Path=BottomText}" FontFamily="Arial" Foreground="White" FontSize="150" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,65"> <TextBlock.Effect> <DropShadowEffect BlurRadius="10" Color="Black" Direction="-90" ShadowDepth="2"></DropShadowEffect> </TextBlock.Effect> </TextBlock> </Grid> </Border> </UserControl>
其中BottomText为自定义属性。
public static readonly DependencyProperty BottomTextProperty = DependencyProperty.Register("BottomText", typeof(string), typeof(MyCardControlBottom), new PropertyMetadata(null)); public string BottomText { get { return (string)GetValue(BottomTextProperty); } set { SetValue(BottomTextProperty, value); } }
2、自定义控件MyCardControlFront
<UserControl x:Class="TurnOverCards.MyCardControlFront" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" x:Name="MyUserControl" Height="150" Width="200"> <Border BorderThickness="0" ClipToBounds="True"> <Border.Background> <RadialGradientBrush GradientOrigin="0.5,0.75" Center="0.5,0.5" RadiusX="0.75" RadiusY="0.75"> <GradientStop Color="DimGray" Offset="0" /> <GradientStop Color="Black" Offset="1" /> </RadialGradientBrush> </Border.Background> <TextBlock Text="{Binding ElementName=MyUserControl,Path=FrontText}" FontFamily="Arial" Foreground="White" FontSize="150" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,65,0,2"> <TextBlock.Effect> <DropShadowEffect BlurRadius="10" Color="Black" Direction="-90" ShadowDepth="2"></DropShadowEffect> </TextBlock.Effect> </TextBlock> </Border> </UserControl>