簡體   English   中英

如何使用純 C# 中的 ObjectAnimationUsingKeyFrames?

[英]How can you use ObjectAnimationUsingKeyFrames from pure C#?

我想從 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);

並得到同樣的錯誤。

行! 我想出了 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”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM