滚动时图像在 SliderView 和 ListView 中消失
Images disappear in SliderView and ListView while scrolling
我有一个 SliderView(属于 AppStudio.Uwp.Controls)。这些图像在我加载页面后立即出现,但在我滚动列表时消失。我也用 ListView 对此进行了测试。那里也发生了同样的事情。
<controls:SliderView x:Name="sliderView" ItemsSource="{x:Bind listForOtherPicturesThumbnails}" ItemTemplate="{StaticResource Hero}"
RelativePanel.Below="mainImage"
RelativePanel.AlignLeftWithPanel="True"
RelativePanel.AlignRightWithPanel="True"
ArrowsVisibility="Visible"
/>
使用的ItemTemplate如下-
<DataTemplate x:Key="Hero" x:DataType="local:StorageItemThumbnailClass">
<Grid Margin="6" Padding="12" Background="White" BorderThickness="1" BorderBrush="LightGray">
<Image Source="{x:Bind Thumbnail, Converter={StaticResource ThumbnailtoImageConverter}}" Stretch="UniformToFill" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</DataTemplate>
在OnNavigated函数中生成缩略图如下-
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
otherPicturesPathList = ((PictureWithList)e.Parameter).pathList;
await PopulateListOfOtherPicturesThumbnailsAsync();
Bindings.Update();
}
private async Task PopulateListOfOtherPicturesThumbnailsAsync()
{
if (otherPicturesPathList != null)
{
List<Task<StorageItemThumbnail>> thumbnailOperations = new List<Task<StorageItemThumbnail>>();
foreach (var path in otherPicturesPathList)
{
var storageFile = await StorageFile.GetFileFromPathAsync(path);
thumbnailOperations.Add(storageFile.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.PicturesView, 100).AsTask());
}
await Task.WhenAll(thumbnailOperations);
for (int k = 0; k < thumbnailOperations.Count; k++)
{
var task = thumbnailOperations[k];
listForOtherPicturesThumbnails.Add(new StorageItemThumbnailClass { Thumbnail = task.Result });
}
}
}
缩略图到图像转换器-
public object Convert(object value, Type targetType, object parameter, string language)
{
BitmapImage image = null;
if (value != null)
{
if (value.GetType() != typeof(StorageItemThumbnail))
{
throw new ArgumentException("Expected a thumbnail");
}
StorageItemThumbnail thumbnail = (StorageItemThumbnail)value;
image = new BitmapImage();
image.SetSource(thumbnail);
}
return (image);
}
StorageItemThumbnailClass-
public class StorageItemThumbnailClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private StorageItemThumbnail _thumbnail;
private string _name;
public StorageItemThumbnail Thumbnail
{
get { return _thumbnail; }
set
{
_thumbnail = value;
// Call OnPropertyChanged whenever the property is updated
OnPropertyChanged("Thumbnail");
}
}
// Create the OnPropertyChanged method to raise the event
protected void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public String Name
{
get { return _name; }
set
{
_name = value;
// Call OnPropertyChanged whenever the property is updated
OnPropertyChanged("Name");
}
}
}
如何解决此问题,使图像在首次加载页面时保持原样?
我无法测试你的代码,但有两个建议:
- 您试过在转换器内部设置断点吗?
- 查看 Visual Studio 的输出面板,也许您可以获得有关您的问题的一些信息
- 将您的代码替换为:
thumbnailOperations.Add(await storageFile.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.PicturesView, 100));
恕我直言,async/await 模式有问题。
我偶然发现了完全相同的问题。
解决方案是在 ThumbnailtoImageConverter
.
中添加 thumbnail.Seek(0);
//...
StorageItemThumbnail thumbnail = (StorageItemThumbnail)value;
thumbnail.Seek(0);
image = new BitmapImage();
image.SetSource(thumbnail);
参考链接:Images disappear in SliderView and ListView while scrolling
我有一个 SliderView(属于 AppStudio.Uwp.Controls)。这些图像在我加载页面后立即出现,但在我滚动列表时消失。我也用 ListView 对此进行了测试。那里也发生了同样的事情。
<controls:SliderView x:Name="sliderView" ItemsSource="{x:Bind listForOtherPicturesThumbnails}" ItemTemplate="{StaticResource Hero}"
RelativePanel.Below="mainImage"
RelativePanel.AlignLeftWithPanel="True"
RelativePanel.AlignRightWithPanel="True"
ArrowsVisibility="Visible"
/>
使用的ItemTemplate如下-
<DataTemplate x:Key="Hero" x:DataType="local:StorageItemThumbnailClass">
<Grid Margin="6" Padding="12" Background="White" BorderThickness="1" BorderBrush="LightGray">
<Image Source="{x:Bind Thumbnail, Converter={StaticResource ThumbnailtoImageConverter}}" Stretch="UniformToFill" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</DataTemplate>
在OnNavigated函数中生成缩略图如下-
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
otherPicturesPathList = ((PictureWithList)e.Parameter).pathList;
await PopulateListOfOtherPicturesThumbnailsAsync();
Bindings.Update();
}
private async Task PopulateListOfOtherPicturesThumbnailsAsync()
{
if (otherPicturesPathList != null)
{
List<Task<StorageItemThumbnail>> thumbnailOperations = new List<Task<StorageItemThumbnail>>();
foreach (var path in otherPicturesPathList)
{
var storageFile = await StorageFile.GetFileFromPathAsync(path);
thumbnailOperations.Add(storageFile.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.PicturesView, 100).AsTask());
}
await Task.WhenAll(thumbnailOperations);
for (int k = 0; k < thumbnailOperations.Count; k++)
{
var task = thumbnailOperations[k];
listForOtherPicturesThumbnails.Add(new StorageItemThumbnailClass { Thumbnail = task.Result });
}
}
}
缩略图到图像转换器-
public object Convert(object value, Type targetType, object parameter, string language)
{
BitmapImage image = null;
if (value != null)
{
if (value.GetType() != typeof(StorageItemThumbnail))
{
throw new ArgumentException("Expected a thumbnail");
}
StorageItemThumbnail thumbnail = (StorageItemThumbnail)value;
image = new BitmapImage();
image.SetSource(thumbnail);
}
return (image);
}
StorageItemThumbnailClass-
public class StorageItemThumbnailClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private StorageItemThumbnail _thumbnail;
private string _name;
public StorageItemThumbnail Thumbnail
{
get { return _thumbnail; }
set
{
_thumbnail = value;
// Call OnPropertyChanged whenever the property is updated
OnPropertyChanged("Thumbnail");
}
}
// Create the OnPropertyChanged method to raise the event
protected void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public String Name
{
get { return _name; }
set
{
_name = value;
// Call OnPropertyChanged whenever the property is updated
OnPropertyChanged("Name");
}
}
}
如何解决此问题,使图像在首次加载页面时保持原样?
我无法测试你的代码,但有两个建议:
- 您试过在转换器内部设置断点吗?
- 查看 Visual Studio 的输出面板,也许您可以获得有关您的问题的一些信息
- 将您的代码替换为:
thumbnailOperations.Add(await storageFile.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.PicturesView, 100));
恕我直言,async/await 模式有问题。
我偶然发现了完全相同的问题。
解决方案是在 ThumbnailtoImageConverter
.
thumbnail.Seek(0);
//...
StorageItemThumbnail thumbnail = (StorageItemThumbnail)value;
thumbnail.Seek(0);
image = new BitmapImage();
image.SetSource(thumbnail);
参考链接:Images disappear in SliderView and ListView while scrolling