Windows RT App中的图像闪烁

我有一个Windows RT应用程序,我以编程方式更改Image组件中的位图。 一切都适用于XAML和下面的代码,除了我们在图像更改时看到的闪烁。

我应该改变什么才能摆脱闪烁?

XAML

                                                   

码:

 namespace Test01 { public sealed partial class MainPage : Page { int currentSlice = 128; String axis = "ax-"; public MainPage() { this.InitializeComponent(); } public void displayImages() { BitmapImage bitmapImage = image1.Source as BitmapImage; bitmapImage.UriSource = null; image1.Source = null; BitmapImage bitmapImage2 = image2.Source as BitmapImage; bitmapImage2.UriSource = null; image2.Source = null; Uri image1Uri = new Uri(this.BaseUri, "BrainImg/axis/" + axis + currentSlice + ".jpg"); image1.Source = new BitmapImage(image1Uri); image2.Source = new BitmapImage(new Uri(this.BaseUri, "BrainImg/aseg/" + axis + currentSlice + ".png")); } protected override void OnNavigatedTo(NavigationEventArgs e) { } private void Slider_ValueChanged_1(object sender, RangeBaseValueChangedEventArgs e) { if (slider1 != null) { currentSlice = (int) slider1.Value; displayImages(); } } } } 

我找到了解决方案。

 IRandomAccessStream stream = await storageFile.OpenAsync(FileAccessMode.Read); BitmapImage bitmapImage = new BitmapImage(); if (await bitmapImage.SetSourceAsync(stream)) { image1.source = bitmapImage; } 

这种方式没有闪烁。

我看到的第一件事是你在将图像源设置为新值之前不必要地将其设置为null,尽管它实际上可能并不重要。 问题是下载/解码图像需要一些时间。 因此,您应该在开始使用之前预先加载所有图像(可能使用太多内存,因此不可行)或使用两个图像控件代替一个图像控件并将它们用作一种交换链,其中您有一个可见图像和一个隐藏的,当你想切换显示的图像时 – 更新隐藏图像的来源,只有在加载位图后才切换图像的可见度。 虽然可能不是直截了当,因为在托管图像控件可见之前BitmapImage可能无法开始加载,因此您可能需要将opacity设置为0.01而不是完全隐藏它或尝试使其可见,然后立即隐藏,直到加载位图。 如果位图已加载 – 其PixelWidth将> 0。