C# C语言中双动画的依赖属性#
溢出 我想为WP编程一个可变动画 我使用表达式混合创建了动画,如下所示:C# C语言中双动画的依赖属性#,c#,animation,windows-phone,dependency-properties,expression-blend,C#,Animation,Windows Phone,Dependency Properties,Expression Blend,溢出 我想为WP编程一个可变动画 我使用表达式混合创建了动画,如下所示: <Storyboard x:Name="rotateC"> <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.Rotation)" Storyboard.TargetName="currentDeviancePointer_s"&
<Storyboard x:Name="rotateC">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.Rotation)" Storyboard.TargetName="currentDeviancePointer_s">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="10"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Window.Resources>
...
<Storyboard x:Key="RotateC">
<DoubleAnimationUsingKeyFrames
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"
Storyboard.TargetName="currentDeviancePointer_s">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="{Binding ValueToRotate}"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
创建了一个对象:
public MainPage()
{
InitializeComponent();
...
angleContainer= new RotateClass();
...
}
我使用以下处理程序创建了一个按钮:
angleContainer.newAngle = 45;
if (rotateC.GetCurrentState() != ClockState.Active)
{
rotateC.Begin();
}
这是我的装订:
<Storyboard x:Name="rotateC">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.Rotation)" Storyboard.TargetName="currentDeviancePointer_s">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="{Binding ElementName=angleContainer,Path=newAngle}"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
有什么错误吗
也许我这样做太难了?还有其他解决办法吗?
谢谢你的帮助 好,假设您的旋转定义如下:
<Storyboard x:Name="rotateC">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.Rotation)" Storyboard.TargetName="currentDeviancePointer_s">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="10"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Window.Resources>
...
<Storyboard x:Key="RotateC">
<DoubleAnimationUsingKeyFrames
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"
Storyboard.TargetName="currentDeviancePointer_s">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="{Binding ValueToRotate}"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
现在,您的故事板指向该标签(通过故事板.TargetName
)
创建依赖项属性:
public static readonly DependencyProperty RotateToValueProperty =
DependencyProperty.Register("RotateToValue", typeof (double), typeof (MainWindow), new PropertyMetadata(default(double)));
public double RotateToValue
{
get { return (double)GetValue(RotateToValueProperty); }
set { SetValue(RotateToValueProperty, value); }
}
您可以在ctor中对其进行初始化,或者将其绑定到其他元素,或者执行任何您想执行的操作
public MainWindow()
{
InitializeComponent();
...
RotateToValue = 45;
...
}
假设这是您的按钮:
<Button Content="Lets rotate something">
<Button.Triggers>
<EventTrigger RoutedEvent="ButtonBase.Click">
<BeginStoryboard Storyboard="{Binding Source={StaticResource RotateC}}"/>
</EventTrigger>
</Button.Triggers>
</Button>
诺蒂斯所说的一切都是正确的!唯一被遗忘的是:
DataContext="{Binding RelativeSource={RelativeSource Self}}"
现在它工作了 为什么有一个类扩展
DependencyObject
。。。您不能简单地绑定到一个DependencyProperty
?您正在使用MVVM吗?有框架吗?我刚刚安装了WP8SDK。没什么特别的。我也尝试过只使用依赖属性,而不使用任何类,但是没有效果。非常感谢。我有一个问题:-如何用C代码触发这个动画?没有x:Name,我无法使用.Begin()。但如果我将x:Key更改为x:Name我的应用程序chrash:)我已经做了所有的事情,但似乎什么都不起作用。如果我将值更改为“10”-一切正常。如果我把它改回binding,什么也不会发生。也许有一些元素名或路径?非常感谢。
((Storyboard)Window_name_here.Resources["RotateC"]).Begin();
DataContext="{Binding RelativeSource={RelativeSource Self}}"