string
storyboardName
=
"
MyStoryBoard
"
;
string myXamlElement = " MyXamlElement " ;
int newLeftPosition = 120 ;
Storyboard sb = XamlReader.Load(String.Format(
@" <Storyboard xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" x:Name=""{0}"">
<DoubleAnimation Storyboard.TargetName=""{1}""
Storyboard.TargetProperty=""(Canvas.Left)""
To=""{2}"" Duration=""00:00:00.1200000""/>
</Storyboard> " , storyboardName, myXamlElement, newLeftPosition)) as Storyboard;
// Add a delegate to remove the storyboard from resources as soon as it is finished.
sb.Completed += new EventHandler(sb_Completed);
// Add to the resources of the page
this .Resources.Add(sb);
// Begin the storyboard which will animate the element to the correct position.
sb.Begin();
string myXamlElement = " MyXamlElement " ;
int newLeftPosition = 120 ;
Storyboard sb = XamlReader.Load(String.Format(
@" <Storyboard xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" x:Name=""{0}"">
<DoubleAnimation Storyboard.TargetName=""{1}""
Storyboard.TargetProperty=""(Canvas.Left)""
To=""{2}"" Duration=""00:00:00.1200000""/>
</Storyboard> " , storyboardName, myXamlElement, newLeftPosition)) as Storyboard;
// Add a delegate to remove the storyboard from resources as soon as it is finished.
sb.Completed += new EventHandler(sb_Completed);
// Add to the resources of the page
this .Resources.Add(sb);
// Begin the storyboard which will animate the element to the correct position.
sb.Begin();
在不少的应用中需要动态的创建动画作出一些复杂的效果。比如说当当拖拽元素是可以简单的用c#代码创建一个storyboard并在这个board中创建一个DoubleAnimation。但是用c#代码创建动画会导致runtime errors因为这还是silverlight的一个bug。但是用 xaml 并load他成为一个简单的storyboard。
xamlReader 对象有一个非常有用的Load()方法。
这里要提醒一点 创建 Storyboard一定要把属性些完整了。 不要漏写了类似x:Name这样的属性。 若够漏写silverlight不会报任何错误。在调试时是直接跳出。
我就犯了这样的错误