C# 加载新图像后,图像框闪烁

C# 加载新图像后,图像框闪烁,c#,image,xaml,windows-runtime,windows-store-apps,C#,Image,Xaml,Windows Runtime,Windows Store Apps,我有一段代码,它为数组中的每个字符串添加文件路径,并将其用作图像框的文件路径,这是代码: private async void Button7_Click(object sender, RoutedEventArgs e) { string[] images = new string[] { "Star_00001.png", "Star_00002.png", "Star_00003.png", "Star_00004.png", "Star_00005.png",

我有一段代码,它为数组中的每个字符串添加文件路径,并将其用作图像框的文件路径,这是代码:

 private async void Button7_Click(object sender, RoutedEventArgs e)
    {
        string[] images = new string[] { "Star_00001.png", "Star_00002.png", "Star_00003.png", "Star_00004.png", "Star_00005.png", "Star_00006.png", "Star_00007.png", "Star_00008.png"};

        string path = "Assets/Star/";

            foreach(string file in images)
            {

            string thepath = Path.Combine(path,file);
            await Task.Delay(46);
            BitmapImage Image = new BitmapImage();
            Image.UriSource = new Uri(this.BaseUri, thepath);
            StarImage.Source = Image;

            }
     }  

现在,每次新图像加载到StarImage时,它都会闪烁,据我所知,他们无法阻止这一点,因为这是在图像框中加载新图像的效果,但是有人知道有没有其他方法可以停止此操作,并提供动画效果

我认为某种缓冲技术可以减少或消除您的闪烁问题

我从来没有在c#中这样做过,但基本上你要画一个还不可见的图像,称为缓冲区,然后在图像完全绘制后使其可见

试试这个:

 private async void Button7_Click(object sender, RoutedEventArgs e)
    {
        string[] images = new string[] { "Star_00001.png", "Star_00002.png", "Star_00003.png", "Star_00004.png", "Star_00005.png", "Star_00006.png", "Star_00007.png", "Star_00008.png" };

        string path = "Assets/Star/";

        foreach (string file in images)
        {

            string thepath = Path.Combine(path, file);
            await Task.Delay(46);

            BitmapImage Image = new BitmapImage();
            Image.BeginInit();
            Image.UriSource = new Uri(this.BaseUri, thepath);
            Image.CacheOption = BitmapCacheOption.OnLoad;
            Image.EndInit();

            StarImage.Source = Image;

        }
    }

为了解决这个问题,我绞尽脑汁好几个月了,然后我找到了这篇文章。它帮我解决了这个问题!我一辈子都不明白为什么微软一直在改变语法来在windows中做事


您的意思是将一些图像加载到不同的图像框中,并使图像框在使用时可见,同时将图像加载到该图像框中,将背景中的其他图像加载到其他图像框中,依此类推?基本上是的。我不太清楚如何在C#中实现这一点。寻找双缓冲或三缓冲的C#技术。这真的是关于WPF的,还是你正在构建一个商店应用程序?我已经编辑了问题标签。Windows应用商店应用不使用WPF。使用BeginInIt()、CacheOption、BitmapCacheOption.OnLoad和EndInit();它是问我是否遗漏了一份推荐信?答案是(正确的)针对WPF给出的。然而,这是行不通的,因为这个问题实际上不是针对WPF,而是针对应用商店。@Clemens啊,我明白了,那完全是我的坏家伙!您自己心里有什么答案吗?您可能会为BitmapImage的
ImageOpened
事件附加一个处理程序,并在那里执行
StarImage.Source
分配。