它应该用作DataTemplate还是ControlTemplate?
<GrID><GrID.Resources> <Storyboard x:Key="LoadingAnimation" RepeatBehavior="Forever"> <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(c:Arc.EndAngle)" Storyboard.Targetname="arc"> <EasingDoubleKeyFrame KeyTime="0" Value="90"/> <EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="-90"/> <EasingDoubleKeyFrame KeyTime="0:0:1.2" Value="-270"/> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(c:Arc.StartAngle)" Storyboard.Targetname="arc"> <EasingDoubleKeyFrame KeyTime="0" Value="-90"/> <EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="-270"/> <EasingDoubleKeyFrame KeyTime="0:0:1.2" Value="-450"/> </DoubleAnimationUsingKeyFrames></Storyboard></GrID.Resources><GrID.Triggers> <EventTrigger RoutedEvent="FrameworkElement.Loaded"> <BeginStoryboard Storyboard="{StaticResource LoadingAnimation}"/></EventTrigger></GrID.Triggers><c:Arc x:name="arcbackground" StartAngle="0" EndAngle="359.9" @R_404_5283@="#FFE0E0E0" @R_404_5283@Thickness="8"/><c:Arc x:name="arc" @R_404_5283@="{StaticResource BlueGradIEntBrush}" @R_404_5283@Thickness="8"/>解决方法 几天前我需要类似的东西.我将故事板和元素放在用户控件中进行动画处理.我添加了一个依赖属性,可以通过bIDing来启动/停止动画.剩下的就是在应用程序中的任何地方使用用户控件.
我的XAML看起来像这样:
<UserControl x:Class="MyAssembly.SpinningWait" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:core="clr-namespace:MyAssembly"> <UserControl.Resources> <Storyboard x:Key="canvasAnimation"> <DoubleAnimationUsingKeyFrames RepeatBehavior="Forever" Speedratio="24" Storyboard.TargetProperty="(UIElement.Rendertransform).(transformGroup.Children)[0].(Rotatetransform.Angle)" Storyboard.Targetname="canvas"> <discreteDoubleKeyFrame KeyTime="0:0:2" Value="45"/> <discreteDoubleKeyFrame KeyTime="0:0:4" Value="90"/> <discreteDoubleKeyFrame KeyTime="0:0:6" Value="135"/> <discreteDoubleKeyFrame KeyTime="0:0:8" Value="180"/> <discreteDoubleKeyFrame KeyTime="0:0:10" Value="225"/> <discreteDoubleKeyFrame KeyTime="0:0:12" Value="270"/> <discreteDoubleKeyFrame KeyTime="0:0:14" Value="315"/> <discreteDoubleKeyFrame KeyTime="0:0:16" Value="360"/> </DoubleAnimationUsingKeyFrames> </Storyboard> </UserControl.Resources> <core:RadialPanel x:name="canvas" RendertransformOrigin="0.5,0.5"> <core:RadialPanel.Resources> <Style targettype="Ellipse"> <Setter Property="Height" Value="6" /> <Setter Property="WIDth" Value="6" /> <Setter Property="Fill" Value="White" /> </Style> </core:RadialPanel.Resources> <core:RadialPanel.Rendertransform> <transformGroup> <Rotatetransform/> </transformGroup> </core:RadialPanel.Rendertransform> <Ellipse /> <Ellipse Opacity="0.1" /> <Ellipse Opacity="0.2" /> <Ellipse Opacity="0.3" /> <Ellipse Opacity="0.4" /> <Ellipse Opacity="0.5" /> <Ellipse Opacity="0.6" /> <Ellipse Opacity="0.7" /> </core:RadialPanel></UserControl>
它的代码隐藏:
public partial class SpinningWait : UserControl{ private Storyboard _storyboard; public SpinningWait() { InitializeComponent(); } public bool IsWaiting { get { return (bool)GetValue(IsWaitingProperty); } set { SetValue(IsWaitingProperty,value); } } public static Readonly DependencyProperty IsWaitingProperty = DependencyProperty.Register("IsWaiting",typeof(bool),typeof(SpinningWait),new UIPropertyMetadata(false,new PropertyChangedCallback(OnIsWaitingChanged))); private static voID OnIsWaitingChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) { ((SpinningWait)d).OnIsWaitingChanged((object)d,e); } private voID OnIsWaitingChanged(object sender,DependencyPropertyChangedEventArgs e) { if (IsWaiting) { this.Visibility = Visibility.Visible; this.StartAnimation(); } else { this.Visibility = Visibility.Collapsed; this.StopAnimation(); } } private voID StartAnimation() { this._storyboard = (Storyboard)FindResource("canvasAnimation"); this._storyboard.Begin(canvas,true); } private voID StopAnimation() { this._storyboard.Remove(canvas); }}总结
以上是内存溢出为你收集整理的c# – WPF中的轻量级处理动画资源?全部内容,希望文章能够帮你解决c# – WPF中的轻量级处理动画资源?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)