将故事板放在应用程序资源中

我需要在几个地方使用相同的故事板,因此我将故事板放在我的Application.Resources中。 当我尝试执行故事板时,唯一的问题是我需要引用我想要动画的目标。 这是我的故事板:

border2          

我为不同对象的高度设置动画的方式是通过更改动态资源目标。 当故事板在当前窗口时,我能够这样做。 但是现在我想将它放在应用程序资源中,我不知道如何引用目标属性。


编辑

我之前发布的解决方案效果不错但有时很难用代码创建复杂的动画。 所以我解决的另一个替代解决方案是使用表达式混合创建故事板。 所以我将一个随机控件拖到表达式混合中的主窗口并创建一个随机动画。 让我们说动画出现为:

                

然后我复制该代码并将其粘贴到我的工作窗口而不是APP.XAML中。

然后在我的代码中让我说我有一个:

           

由于某种原因,变换组必须在那里才能为对象设置动画。 无论如何,让我们说我在工作窗口中有那个寄宿生,我想用我用表达式混合创建的相同动画来设置动画。 我将在代码中做的是:

 Storyboard sb1 = FindResource("Storyboard1") as Storyboard; foreach (var child in sb1.Children) { Storyboard.SetTargetName(child, brdBorder.Name); } sb1.Begin(this); 

然后我能够在我的工作窗口上设置该边框的动画。 这个很好的部分是我能够将相同的动画应用于多个对象(这是我认为创建资源的目的)当我尝试将故事板放在资源字典或app.xaml中时问题就出现了。文件。 当我这样做时,c#能够找到故事板,但故事板的属性是只读的,因此我得到错误:

 Cannot set a property on object 'System.Windows.Media.Animation.DoubleAnimationUsingKeyFrames' because it is in a read-only state. 

我想要这样做的原因是将相同的动画应用于多个对象。 解决方案是使用代码构建基本动画,然后使用更复杂的动画(如缓动函数等)将其另存为资源。 让我告诉你我的意思。

在我的资源文件中,我放置了以下资源:

       

在expresion混合中,您可以构建更复杂的缓动function。 然后使用代码我将创建一个基本的故事板:

 DoubleAnimation animation = new DoubleAnimation(); animation.To = 336; // final value //animation.From = 0; //animation.BeginTime = TimeSpan.FromSeconds(0); animation.Duration = new Duration(TimeSpan.FromSeconds(5)); // how much time should animation last // here comes the magic: // note that I can bind to EasingDoubleKeyFrame in my resource file in xaml animation.EasingFunction = ((EasingDoubleKeyFrame)FindResource("pleaseWork")).EasingFunction; // apply the easing function Storyboard.SetTarget(animation, groupBox1); // what object will be animated? Storyboard.SetTargetProperty(animation, new PropertyPath(FrameworkElement.HeightProperty)); // what property will be animated Storyboard sb = new Storyboard(); sb.Children.Add(animation); sb.Begin(); 

这使我能够在多个对象上使用相同的故事板。

您无需在任何地方引用目标,您只需在本地创建一个目标,这是动态资源查找的重要function之一,例如

             
   

最后我找到了解决方案!!!!!

如果你还记得我在app.xaml文件中放置故事板时得到的错误是:

无法在对象’System.Windows.Media.Animation.DoubleAnimationUsingKeyFrames’上设置属性,因为它处于只读状态。

换句话说,我无法修改故事板的目标属性,我可以将其更改为红色。 所以解决方案是为Storyboard.TargetName="{binding}"更改Storyboard.TargetName="grid" Storyboard.TargetName="{binding}"


让我把所有东西放在一起:

 step 1: // create your custom storyboard with expression blend or xaml: // let's say it comes out as:               

 step 2: // copy and paste your storyboard to your app.xaml file or to a // resource dictionary (if you paste it in a resource dictionary do not // forget to merge the dictionaries so that your code is able to find the // storyboard as a resource)      

-

 step 3 

//替换Storyboard.TargetName =“{Binding}”的Storyboard.TargetName =“grid”。 //你的故事板资源不应该是这样的:

                

-

 step 4 //create a method to make it easy to run the same animation on multiple objects: void runStoryboard(string storyboardName, string objectName) { Storyboard sb = FindResource(storyboardName) as Storyboard; foreach (var child in sb.Children) Storyboard.SetTargetName(child, objectName); sb.Begin(this); // do not forget the this keyword } 

-

 step 5 // start your animation with the object you wish to animate runStoryboard("Storyboard1", brdBorder.Name); 

注意重要事项:

通过意识到在使用表达式混合创建故事板时,有时表达式混合将为您要设置动画的控件创建渲染变换组。 在这个例子中,我为一个边界设置了动画。 并且对于那个需要动画的边框来说

          

换句话说,如果您的动画处理scaletransform skewtransform等,则将该渲染变换组放置到您计划制作动画的所有对象上。


最后,我能够动画:

           // AND  AS: runStoryboard("Storyboard1", brdBorder.Name); runStoryboard("Storyboard1", mybutton.Name); 

为一个

Windows Phone 8.1应用程序

我遇到了同样的问题,我添加混合许多解决方案来实现这一目标。

这是我的App.xaml文件

         

在我的页面中,我有这样的东西( page.xaml

  //Etc ....  

现在,您将此方法绑定到事件(按钮点击或其他)

page.xaml.cs

 private void MenuPanel_OnTap(object sender, GestureEventArgs e) { Storyboard sb = App.Current.Resources["ShowMenuAnimationKey"] as Storyboard; sb.Stop(); Storyboard.SetTarget(sb, ViewPanel); sb.Begin(); } 

我希望这会有所帮助!