5

我在Metro XAML应用程序中混淆了一些Storyboard。我必须在代码中创建一个Storyboard。我想设置Storyboard.TargetPropertyCompositeTransform.Rotation无法将Storyboard.TargetProperty设置为Metro风格应用程序中的CompositeTransform.Rotation,代码为

这似乎是不可能的......

我的故事板在XAML看起来是这样的:

<Storyboard> 
    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.Rotation)" Storyboard.TargetName="grid"> 
     <EasingDoubleKeyFrame KeyTime="0" Value="0"/> 
     <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="60"/ 
    </DoubleAnimationUsingKeyFrames> 
</Storyboard> 

我想创建类似的东西。
重要提示:我不想重新创建这个确切的故事板。我在自定义ContentControl的代码中,所以thisControl,并且没有“网格”来定位动画。目标是控制本身,其中有CompositeTransform以前设置。

到目前为止我的代码是这样的:

var turnSB = new Storyboard(); 

var doubleAnim = new DoubleAnimationUsingKeyFrames(); 
doubleAnim.KeyFrames.Add(new EasingDoubleKeyFrame() { KeyTime = TimeSpan.FromMilliseconds(0), Value = currentAngle }); 
doubleAnim.KeyFrames.Add(new EasingDoubleKeyFrame() { KeyTime = TimeSpan.FromMilliseconds(500), Value = targetAngle }); 

turnSB.Children.Add(doubleAnim); 

Storyboard.SetTarget(doubleAnim, this.RenderTransform); 
Storyboard.SetTargetProperty(doubleAnim, "(CompositeTransform.Rotation)"); 

turnSB.Begin(); 

只要它击中Begin方法我得到一个异常说(CompositeTransform.Rotation)不能得到解决。所以我猜我没有得到物业路径非常正确。我尝试了不同的变体,但根据PropertyPaths,这应该是正确的,不是吗? :S

如果这是一个无法解决的问题,我愿意在解决方法建议...

编辑:

我想我已经解决了这个问题现在。我有一些有趣的发现,虽然...

如果我做一个用户控件,我几乎可以做任何事情。一切正常,我可以设置Storyboard.Target属性,并且动画可以正确播放。

但是,如果我使用自定义控件,或从另一个控件(比如ContentControl)继承,我无法从代码启动Storyboard,仅在某些情况下。

例如:如果我制作一个Storyboard(在XAML中定义)来为Rotation(或任何转换属性)制作动画并尝试从代码开始,我会得到上述异常。但是,如果我为一个简单的属性创建动画,比如说不透明度,它就可以正常工作。
(我用UserControl做了同样的工作,它工作。)

有人可以解释这个吗?

+0

我有同样的问题,有从电网控制一个类继承,故事简单地赢得”开始。 – 2013-01-16 09:40:13

+0

有一点需要注意的是,Grid不是从Control继承的,所以它不是一个真正的自定义控件。 – 2013-01-18 05:27:05

+0

我没有使用Grid,我从ContentControl继承。 xaml来自之前的尝试,我有一个拥有网格的UserControl,并且这是动画的目标。但是对于这种情况,目标应该是控制本身。其中 - 如果你仔细阅读 - 我在'重要'一节中描述。 – Tenshiko 2013-01-29 16:37:20

回答

4

MSDN docs看起来您需要设置整个字符串路径。因此,对于在XAML中所描述的动画,你就需要设置TargetProperty这样

Storyboard.SetTargetProperty(doubleAnim, "(UIElement.RenderTransform).(CompositeTransform.Rotation)"); 

UPDATE: 发现这个blog post它增加了一个时间轴的故事板的一个孩子。请尝试以下操作:

Storyboard.SetTarget(doubleAnim, this.RenderTransform); 
Storyboard.SetTargetProperty(doubleAnim, "Rotation"); // maybe "CompositeTransform.Rotation" 
storyboard.Children.Add(doubleAnim); 
+0

我试过这些的每一个组合。结果总是一样的。 – Tenshiko 2012-08-18 19:18:34

+0

我已经得出结论,问题不在故事板设置中。事实上,您的第一个建议现在可以使用,但只能使用UserControls。我想了解更多关于这个主题的内容,也许有人可以对此有所了解。但如果不是,我会接受你的答案。 Thx :) – Tenshiko 2012-08-18 19:36:05

+0

那么这是在一个自定义控件内? – 2012-08-19 04:48:20

4

我认为你得到这个错误的原因是因为你没有实例化的自定义控件的RenderTransform财产。

public class CustomControl2 : Control 
{ 
    public CustomControl2() 
    { 
     this.DefaultStyleKey = typeof(CustomControl2); 
    } 

    protected override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 
    } 

    public void RunAnimation() 
    { 
     //this.RenderTransform = new CompositeTransform(); 
     this.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0x33, 0xC8, 0x9C)); 

     var turnSB = new Storyboard(); 

     var doubleAnim = new DoubleAnimationUsingKeyFrames(); 
     doubleAnim.KeyFrames.Add(new EasingDoubleKeyFrame() { KeyTime = TimeSpan.FromMilliseconds(0), Value = 10 }); 
     doubleAnim.KeyFrames.Add(new EasingDoubleKeyFrame() { KeyTime = TimeSpan.FromMilliseconds(500), Value = 30 }); 

     turnSB.Children.Add(doubleAnim); 

     Storyboard.SetTarget(doubleAnim, this.RenderTransform); 
     Storyboard.SetTargetProperty(doubleAnim, "(CompositeTransform.Rotation)"); 

     turnSB.Begin(); 
    } 
} 

注意,在上面的代码,如果我的方法RunAnimation下注释掉的第一行,它会抛出我,你所得到的是同样的错误。

然后我在我的主页中创建了这个控件,并创建了一个Button来启动动画。

private void Button_Click_1(object sender, RoutedEventArgs e) 
{ 
    this.MyControl.RunAnimation(); 
} 

我测试了上面的代码,它工作正常。

+0

我明白了为什么这应该起作用,但正如我在我的原始文章中说过的,我设置了转换(请再次阅读“重要”部分)。而且,我能用更简单的解决方案重现相同的效果。让我再检查一次,然后回到你身边。 – Tenshiko 2013-01-29 16:30:19

+0

我能想到的一件事是,也许你在调用“OnApplyTemplate”之前启动了分区? – 2013-01-30 03:49:13

1

解决它

的问题是在路径的youre使用元素,其具有从其延伸到所述属性本身的父类派生。我有我自己的控制工作,所以做了一个小例子,你可以复制粘贴(未经测试的代码):

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Media; 
using Windows.UI.Xaml.Media.Animation; 

namespace CustomControls 
{ 
    /// <summary> 
    /// Author: Frank Wolferink 
    /// Note: example is untested and comes as is. I hope it save's you the time i lost figering this out 
    /// </summary> 
    public class CustomControl : Control 
    { 

     private Storyboard _compositeTransformExampleStoryBoard; 

     private const string TRANSLATE_X_TARGET = "Control.RenderTransform.CompositeTransform.TranslateX"; 
     private const string TRANSLATE_Y_TARGET = "Control.RenderTransform.CompositeTransform.TranslateY"; 
     private const string TRANSLATE_ROTATE_TARGET = "Control.RenderTransform.CompositeTransform.Rotation"; 


     public CustomControl() 
     { 
      this.RenderTransform = new CompositeTransform(); 

      TimeSpan duration = new TimeSpan(0,0,0,0,500); 
      double translateX = 10; 
      double translateY = 10; 
      double rotation = 40; 

      _compositeTransformExampleStoryBoard = BuildStoryboard(duration, translateX, translateY, rotation); 

      this.Loaded += CustomControl_Loaded; 
     } 

     void CustomControl_Loaded(object sender, RoutedEventArgs e) 
     { 
      _compositeTransformExampleStoryBoard.Begin(); 
     } 


     private Storyboard BuildStoryboard(TimeSpan animationDuration, double transistionValueX, double transistionValueY, double rotation) 
     { 
      Storyboard storyboard = new Storyboard(); 

      if (transistionValueX != 0) 
       CreateAnimation(storyboard, transistionValueX, animationDuration, TRANSLATE_X_TARGET); 

      if (transistionValueY != 0) 
       CreateAnimation(storyboard, transistionValueY, animationDuration, TRANSLATE_Y_TARGET); 

      if (rotation != 0) 
       CreateAnimation(storyboard, rotation, animationDuration, TRANSLATE_ROTATE_TARGET); 


      return storyboard; 
     } 

     private void CreateAnimation(Storyboard storyboard, double transistionValue, TimeSpan animationDuration, string targetProperty) 
     { 
      DoubleAnimation da = CreateDoubleAnimation(transistionValue, animationDuration); 
      storyboard.Children.Add(da); 
      Storyboard.SetTarget(da, this); 
      Storyboard.SetTargetProperty(da, targetProperty); 
     } 

     private DoubleAnimation CreateDoubleAnimation(double transistionValue, TimeSpan duration) 
     { 
      return new DoubleAnimation() 
      { 
       Duration = duration, 
       To = transistionValue 
      }; 
     } 

    } 
} 

相关问题