1

我想从 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>

但我无法弄清楚与此 XAML 等效的 C#。具体来说,我想从 C# 更改在 Image 对象中显示的图像。

我试过这个:

        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();

并且我收到错误“附加信息:不存在适用的名称范围来解析名称‘图像’。”

在我的控件的 XAML 中,x:name 是“图像”

<Image x:Name="image" ... />

我也试过

Storyboard.SetTargetName(animation, image.Name);

并得到同样的错误。

4

1 回答 1

0

好的!我想出了 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();

我使用“SetTarget”而不是“SetTargetName”

于 2013-01-23T06:40:04.133 回答