滚动时,图像在SliderView和ListView中消失

时间:2016-07-15 17:45:29

标签: c# xaml listview uwp-xaml

我有一个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");
        }
    }

}

如何解决此问题,以便在首次加载页面时图像保持不变?

2 个答案:

答案 0 :(得分:2)

我偶然发现了同样的问题 解决方案是在thumbnail.Seek(0);中添加ThumbnailtoImageConverter

//...
StorageItemThumbnail thumbnail = (StorageItemThumbnail)value;
thumbnail.Seek(0);
image = new BitmapImage();
image.SetSource(thumbnail);

RefLink:Images disappear in SliderView and ListView while scrolling

答案 1 :(得分:0)

我无法测试您的代码,但有两个建议:

  • 您是否尝试在Converter中设置断点?
  • 查看Visual Studio的“输出”面板,也许您可​​以获得有关您的问题的一些信息
  • 用以下代码替换您的代码:

thumbnailOperations.Add(await storageFile.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.PicturesView, 100));

Imho有异步/等待模式的错误。