c# - 在 WPF 中显示动画 gif

标签 c# wpf user-interface xaml animation

我想在我的程序进行时在我的 XAML 中显示动画 gif,例如加载...。我发现这在 WPF 中不容易完成,因为我加载了我的 Gif,它只显示了第一帧。在 WPF 中显示动画的最佳方式是什么。

最佳答案

我有这个问题,直到我发现在 WPF4 中,您可以模拟您自己的关键帧图像动画。首先,将您的动画分成一系列图像,将它们命名为“Image1.gif”、“Image2,gif”等。将这些图像导入您的解决方案资源。我假设您将它们放在图像的默认资源位置。

您将使用图像控件。使用以下 XAML 代码。我已经删除了非必需品。

<Image Name="Image1">
   <Image.Triggers>
      <EventTrigger RoutedEvent="Image.Loaded"
         <EventTrigger.Actions>
            <BeginStoryboard>
               <Storyboard>
                   <ObjectAnimationUsingKeyFrames Duration="0:0:1" Storyboard.TargetProperty="Source" RepeatBehavior="Forever">
                      <DiscreteObjectKeyFrames KeyTime="0:0:0">
                         <DiscreteObjectKeyFrame.Value>
                            <BitmapImage UriSource="Images/Image1.gif"/>
                         </DiscreteObjectKeyFrame.Value>
                      </DiscreteObjectKeyFrames>
                     <DiscreteObjectKeyFrames KeyTime="0:0:0.25">
                        <DiscreteObjectKeyFrame.Value>
                           <BitmapImage UriSource="Images/Image2.gif"/>
                        </DiscreteObjectKeyFrame.Value>
                     </DiscreteObjectKeyFrames>
                     <DiscreteObjectKeyFrames KeyTime="0:0:0.5">
                        <DiscreteObjectKeyFrame.Value>
                           <BitmapImage UriSource="Images/Image3.gif"/>
                        </DiscreteObjectKeyFrame.Value>
                     </DiscreteObjectKeyFrames>
                     <DiscreteObjectKeyFrames KeyTime="0:0:0.75">
                        <DiscreteObjectKeyFrame.Value>
                           <BitmapImage UriSource="Images/Image4.gif"/>
                        </DiscreteObjectKeyFrame.Value>
                     </DiscreteObjectKeyFrames>
                     <DiscreteObjectKeyFrames KeyTime="0:0:1">
                        <DiscreteObjectKeyFrame.Value>
                           <BitmapImage UriSource="Images/Image5.gif"/>
                        </DiscreteObjectKeyFrame.Value>
                     </DiscreteObjectKeyFrames>
                  </ObjectAnimationUsingKeyFrames>
               </Storyboard>
            </BeginStoryboard>
         </EventTrigger.Actions>
      </EventTrigger>
   </Image.Triggers>
</Image>

关于c# - 在 WPF 中显示动画 gif,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1520381/

相关文章:

c# - 为什么 Environment.NewLine 在我的 String.Replace() 中不起作用,但 "\n"却起作用?

c# - 具有私有(private) setter 的 EFCore Base 实体模型属性 - 迁移异常行为

c# - 如何设置展开多少层TreeView节点?

c# - WPF 'Map' 控件;使用 Canvas 显示大量位图图像

linux - linux的GUI开发工具比较

java - java中带有类型属性的JSON到XML

项目定义为数据模板的 WPF 上下文菜单

看起来像 3DSMAX 的 WPF 3D 编辑器

c# - WinForms 中的 WPF : blocked on Dispatcher. 尽管 UI 线程处于事件状态但仍调用

java - 在 Java 中使用箭头键移动图像