在WPF中显示动画gif

par*_*oir 10 c# wpf user-interface animation xaml

我希望在我的XAML中显示动画gif,例如loading ...,因为我的程序正在进行中.我发现这在WPF中不能轻易完成,因为我加载了我的Gif并且它只显示了第一帧.在WPF中显示动画的最佳方法是什么?

Cod*_*e92 5

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

您将使用Image控件.使用以下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>
Run Code Online (Sandbox Code Playgroud)


Mat*_*cey 2

您可以嵌入 MediaElement

<MediaElement LoadedBehavior="Play" Source="path/to.file" />
Run Code Online (Sandbox Code Playgroud)

或 winforms PictureBox:

    <wfi:WindowsFormsHost>
        <winForms:PictureBox x:Name="pictureBoxLoading">
        </winForms:PictureBox>
    </wfi:WindowsFormsHost>
Run Code Online (Sandbox Code Playgroud)

不过,我建议在 WPF 中找到一种方法来执行此操作。看看故事板和动画。如果不知道您想要实现什么目标或为什么要这样做,就很难提供进一步的建议。