WPF中全局开启或关闭动画效果
公司开发的WPF项目中,要求有这样一个业务:用户可以开启或关闭动画效果。WPF的绚丽之处就是3D、动画、特效等等,给人非常炫的感觉,但是机器配置较差的用户可能需要关闭动画特效。
我甚至遇到一个3D特效在显卡不好机器上,导致应用程序崩溃的情况。可见WPF对机器的要求还是比较高的。如果才能做到呢?需要以下几步就能轻松完成。
初始状态:
鼠标移动到矩形上颜色更改
1.建立两个资源字典,分别为Storyboards.xaml和NonStoryboards.xaml
见项目文件截图:
Storyboards.xaml里放有动画的内容
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Storyboard x:Key="Storyboard1">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="rectangle">
<EasingColorKeyFrame KeyTime="0:0:0.5" Value="Yellow"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="Storyboard2">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="rectangle">
<EasingColorKeyFrame KeyTime="0" Value="Yellow"/>
<EasingColorKeyFrame KeyTime="0:0:0.5" Value="#FF6B6B9B"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</ResourceDictionary>
NonStoryboards.xaml里放有没有动画的内容,并保证Key一致
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Storyboard x:Key="Storyboard1">
</Storyboard>
<Storyboard x:Key="Storyboard2">
</Storyboard>
</ResourceDictionary>
2.在App.xaml.cs增加一个静态方法,加载资源字典,定义在App中,好处是应用程序级别设置,而不是针对某个窗体
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
namespace AnimationSwitch {
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application {
protected override void OnStartup(StartupEventArgs e) {
base.OnStartup(e);
App.LoadResources(true);
}
public static void LoadResources(bool allowStoryboard) {
string resFilename = "/Res/" + (allowStoryboard ? "Storyboards.xaml" : "NonStoryboards.xaml");
ResourceDictionary resDic = new ResourceDictionary {
Source = new Uri(resFilename, UriKind.Relative)
};
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(resDic);
}
}
}
3.在窗体设置对象与对象的动画,以及动画效果的CheckBox开关
这里要注意的是:Storyboard="{DynamicResource Storyboard1}必须使用DynamicResource(动态资源),如果使用了StaticResource则第一次加载后,不会更改。
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="AnimationSwitch.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseEnter" SourceName="rectangle">
<BeginStoryboard Storyboard="{DynamicResource Storyboard1}"/>
</EventTrigger>
<EventTrigger RoutedEvent="Mouse.MouseLeave" SourceName="rectangle">
<BeginStoryboard Storyboard="{DynamicResource Storyboard2}"/>
</EventTrigger>
</Window.Triggers>
<Grid>
<Rectangle x:Name="rectangle" HorizontalAlignment="Left" Margin="79,43,0,0" Stroke="Black" Width="162" Fill="#FF6B6B9B" Height="99" VerticalAlignment="Top" RadiusY="22.5" RadiusX="22.5" d:LayoutOverrides="VerticalAlignment" />
<CheckBox Content="动画效果" HorizontalAlignment="Left" Margin="99,178,0,0" VerticalAlignment="Top" IsChecked="True" Width="80" Click="CheckBox_Click" />
</Grid>
</Window>
CheckBox事件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace AnimationSwitch {
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
private void CheckBox_Click(object sender, RoutedEventArgs e) {
CheckBox checkBox=sender as CheckBox;
App.LoadResources(checkBox.IsChecked.Value);
}
}
}
到此大功告成!
源代码下载:http://download.****.net/source/3349597