wpf 如何使用纯 C# 中的 ObjectAnimationUsingKeyFrames?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14473458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can you use ObjectAnimationUsingKeyFrames from pure C#?
提问by Thrill Science
I want to dynamically, from C#, do something like this:
我想从 C# 动态地做这样的事情:
<ObjectAnimationUsingKeyFrames BeginTime="00:00:00"
Storyboard.TargetName="image"
Storyboard.TargetProperty="(Image.Source)">
<DiscreteObjectKeyFrame KeyTime="00:00:00.7000000">
<DiscreteObjectKeyFrame.Value>
<BitmapImage UriSource="check_24.png" />
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
but I can't figure out the C# equivalent to this XAML. Specifically, I want to, from C#, change the image that's displayed in an Image object.
但我无法弄清楚与此 XAML 等效的 C#。具体来说,我想从 C# 更改显示在 Image 对象中的图像。
I tried this:
我试过这个:
ObjectAnimationUsingKeyFrames animation = new ObjectAnimationUsingKeyFrames();
animation.BeginTime = TimeSpan.FromSeconds(0);
Storyboard.SetTargetName(animation, "image");
Storyboard.SetTargetProperty(animation, new PropertyPath("(Image.Source)"));
DiscreteObjectKeyFrame keyFrame = new DiscreteObjectKeyFrame(BitmapFrame.Create(uri), TimeSpan.FromSeconds(0.7));
animation.KeyFrames.Add(keyFrame);
myStoryboard.Children.Add(animation);
myStoryboard.Begin();
and I get the error "Additional information: No applicable name scope exists to resolve the name 'image'."
我收到错误“附加信息:不存在适用的名称范围来解析名称‘图像’。”
In my XAML for the controls, the x:name is "image"
在我的控件 XAML 中,x:name 是“图像”
<Image x:Name="image" ... />
I also tried
我也试过
Storyboard.SetTargetName(animation, image.Name);
Storyboard.SetTargetName(animation, image.Name);
and got the same error.
并得到同样的错误。
回答by Thrill Science
Ok! I figured out the C# equivalent.
好的!我想出了 C# 等价物。
ObjectAnimationUsingKeyFrames animation = new ObjectAnimationUsingKeyFrames();
animation.BeginTime = TimeSpan.FromSeconds(0);
Storyboard.SetTarget(animation, image);
Storyboard.SetTargetProperty(animation, new PropertyPath("(Image.Source)"));
DiscreteObjectKeyFrame keyFrame = new DiscreteObjectKeyFrame(BitmapFrame.Create(uri), TimeSpan.FromSeconds(0.7));
animation.KeyFrames.Add(keyFrame);
myStoryboard.Children.Add(animation);
myStoryboard.Begin();
I used "SetTarget" instead of "SetTargetName"
我使用了“SetTarget”而不是“SetTargetName”

