【问题标题】:Dependency Property for double Animation in C#C#中双动画的依赖属性
【发布时间】:2023-03-04 20:24:01
【问题描述】:

溢出!

我想为 WP 编写一个变量 Animation。

我用 Expression Blend 像这样创建了动画:

<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>

现在我想将第二个值绑定到一个变量。

我找到了 Dependency Properties 的教程并进行了尝试。我的代码编译并运行,但没有任何反应:(

这是我所做的:

创建的依赖对象:

  public class RotateClass : DependencyObject
  {
    public static readonly DependencyProperty NewAngleProperty =
        DependencyProperty.Register("newAngle", typeof(double), typeof(RotateClass), new PropertyMetadata(10.0));

    public double newAngle
    {
        get
        {
            return (double)GetValue(NewAngleProperty);
        }
        set
        {
            SetValue(NewAngleProperty, (double)value);
        }
    }
}

创建了一个对象:

    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>

有什么错误吗?

也许我这样做太难了?还有其他解决方案吗? 感谢您的帮助!

【问题讨论】:

  • 为什么你有一个扩展 DependencyObject 的类...你不能简单地绑定到 DependencyProperty 吗?你在使用 MVVM 吗?有什么框架吗?
  • 我刚刚安装了 WP8 SDK。没什么特别的。我也试过只使用依赖属性,不使用任何类,但它不起作用。

标签: c# animation windows-phone dependency-properties expression-blend


【解决方案1】:

好的,假设你的轮换是这样定义的:

<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>

首先,请注意这里需要x:Key,而不是名称。这在您的窗口或用户控件资源中,在根元素之前。 其次,请注意我将值绑定到ValueToRotate。这将是您将附加到的 DependencyProperty。
我还设置了Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle),所以它会改变元素的角度。

您需要有一个名称为currentDeviancePointer_s 的元素,因为这是您在Storyboard.TargetName 中的内容。假设您定义了Label

<Label Name="currentDeviancePointer_s" .../>

现在您的情节提要指向该标签(通过Storyboard.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>

您可以看到它有一个触发器,一旦单击按钮,就会启动我们在资源中定义的故事板。
该情节提要针对标签(将元素更改为您想要的任何内容,只需确保您的情节提要指向正确的元素),在我们的例子中您会看到它旋转 45 度(因为我们将 RotateToValue 设置为 45。

完成:)。

编辑: 如果你坚持使用开始: 1. 确保你的窗口有名字:

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Name="Window_name_here">

你不需要Storyboard 上的名字,x:Key 就足够了。我假设它仍然是RotateC

在你的代码后面,在不同的按钮点击中使用它(这样你就可以看到它工作):

((Storyboard)Window_name_here.Resources["RotateC"]).Begin();

【讨论】:

  • 非常感谢。我有一个问题: - 我如何从 C# 代码中触发这个动画?没有 x:Name 我不能使用 .Begin()。但是如果我将 x:Key 更改为 x:Name 我的应用程序会崩溃:)
  • 我已经做了一切,但似乎没有任何效果。如果我将值更改为“10” - 一切正常。如果我将其更改回绑定,则不会发生任何事情。也许有 ElementName 或 Path 的东西?谢谢!
【解决方案2】:

Noctis 所说的一切都是正确的!唯一忘记的是:

DataContext="{Binding RelativeSource={RelativeSource Self}}"

现在可以了!

【讨论】:

    猜你喜欢
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多