简体   繁体   English

C#XAML Metro图像动态源

[英]C# XAML Metro Image dynamic Source

I like to do simple animation using one Image control in XAML and swaping Source of Image to other in interval 1 second. 我喜欢使用XAML中的一个Image控件并在1秒的间隔内将Image Source交换给另一个来做简单的动画。

But when I do that, the image is flickering. 但是当我这样做时,图像会闪烁。 I using this code (at Tick event handler of Timer ): 我使用此代码(在Tick的事件处理程序Timer ):

Uri uri = new Uri("ms-appx:/Sprites/Running/" + y++ + ".png", 
              UriKind.RelativeOrAbsolute);
BitmapImage textureBitmap = new BitmapImage(uri);

this.ImageHolder.Source = textureBitmap;

Where can the problem be ? 问题出在哪里? Should I cache BitmapImages? 我应该缓存BitmapImages吗?

Thank you for your help. 谢谢您的帮助。

Try to declare a StoryBoard in your xaml: 尝试在您的xaml中声明一个StoryBoard

    <Storyboard>
        <ObjectAnimationUsingKeyFrames BeginTime="0:0:0" 
            Storyboard.TargetName="ImageHolder" Storyboard.TargetProperty="Source">
            <DiscreteObjectKeyFrame KeyTime="0:0:0" 
               Value="{Binding Source={StaticResource Frame1Image}"/>
            <DiscreteObjectKeyFrame KeyTime="0:0:1" 
               Value="{Binding Source={StaticResource Frame2Image}"/>
            <DiscreteObjectKeyFrame KeyTime="0:0:2" 
               Value="{Binding Source={StaticResource Frame3Image}"/>
            <!-- etc -->
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>

So, you have to add sprites to static resources, and run a StoryBoard. 因此,您必须将精灵添加到静态资源中,然后运行StoryBoard。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM