2

UWP 和 XAML 相当新,我不确定如何调试它。

我有一个基于 Template10 Hamburger 模板和 Template10 增量加载示例的 UWP 应用程序,以及来自示例照片查看器的一些位(Windows 8: Making a Simple Photo Viewer in C# and XAML)。

我已经修改了主页以显示图片文件夹中图像的 Gridview,并以增量方式加载图像。我还从示例照片查看器中提取了一些内容(Windows 8: Making a Simple Photo Viewer in C# and XAML)。

当应用程序启动时,图像会按预期显示,当我向下滚动时,图像会按需加载和显示。问题是当我向上滚动列表时,图像不再显示。我的 gridview 项目仍然存在,显示文件名和彩色项目背景,但不再绘制图像。

为了保持我的内存占用很小,我没有将实际的位图图像存储为我的收藏的一部分,而是存储一个 StorageItemThumbnail。我最初只想存储图像路径,但这不适用于图片库中的任何内容。

public class Picture
{
    public StorageItemThumbnail  ImageThumbNail {get; set;}
    public string Name {get; set;}
}

为了显示这一点,我使用了一个转换器类来创建一个流来设置图像源:

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 StorageItemThumbnail as binding input.");
        }
        if (targetType != typeof(ImageSource))
        {
            throw new ArgumentException("Expected ImageSource as binding output.");
        }

        if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
        {
            image = new BitmapImage();
            image.UriSource = new Uri("ms-appx:///Assets/DesignModeThumbnailPlaceholder.png");
        }
        else
        {
            StorageItemThumbnail ThumbNailFile = (StorageItemThumbnail)value;

            if (ThumbNailFile == null)
                return image;

            image = new BitmapImage();
            IRandomAccessStream thumbnailStream = ThumbNailFile as IRandomAccessStream;
            image.SetSource(thumbnailStream);
        }

    }

    return (image);

}

这在我的 XAML 中绑定如下:

    <DataTemplate x:Name="PictureGridTemplate2">
    <Grid Height="150" Width="150">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Border Grid.RowSpan="2" Background="Black" Opacity="0.8" />
        <Image Width ="130" HorizontalAlignment="Center"
               Stretch="Uniform" Source="{Binding ImageThumbNail, Converter={StaticResource ThumbnailFileToImageSourceConverter}}"/>
        <TextBlock Grid.Row="1" MaxHeight="30" Text="{Binding Name}"
                   Foreground="White" TextTrimming="CharacterEllipsis"/>
    </Grid>
</DataTemplate>

谁能指出我做错了什么的方向?

雪莉

* 解决 *

我终于能够弄清楚是什么导致了这个问题。

这是 gridview 虚拟化、我的数据模型以及我如何通过转换器提供图像的副作用。

作为测试,我删除了转换器,更改了我的数据模型以存储缩略图的 BitmapImage 实例(小于存储整个图像)并直接绑定到该属性。这很有效,当我在我的网格视图中上下滚动时,屏幕上显示的图像。

然后,我更改了我的数据模型,让 BitmapImage 属性获取器从 StorageItemThumbnail 属性动态返回 BitmapImage 构建 - 与使用转换器时的问题相同。

通过在 getter 中添加一些调试语句,我看到第二个请求中 BitmapImage 的高度和宽度为 0。啊哈!那为什么是0?

查看第二个请求的 StorageItemThumbnail 属性,我看到 Stream 位置位于 EOF(不像第一个请求中那样为 0) - 所以这解释了 0 宽度和高度,这解释了屏幕上的空图像控件。

我将代码更改为使用 StorageItemThumbnail.CloneStream,现在显示所有图像。

现在是转换器方法:

    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 StorageItemThumbnail as binding input.");
            }
            if (targetType != typeof(ImageSource))
            {
                throw new ArgumentException("Expected ImageSource as binding output.");
            }

            if ((StorageItemThumbnail)value == null)
            {
                System.Diagnostics.Debug.WriteLine("Thumbnail is null.");
                return image;
            }

            image = new BitmapImage();

            using (var thumbNailClonedStream = ((StorageItemThumbnail)value).CloneStream())
            {
                System.Diagnostics.Debug.WriteLine("Setting image source from cloned stream.");
                image.SetSource(thumbNailClonedStream);
            } 
        }

        return (image);

    }

感谢所有花时间回答并帮助我指出正确方向的人。

4

1 回答 1

0

您可以尝试使用已编译的绑定x:Bind而不是 Binding
在这种情况下,您的应用程序性能会更快。

使用阶段优化您的数据模板以逐步更新 GridView 项目:

    <Grid Height="150" Width="150">
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Border Grid.RowSpan="2" Background="Black" Opacity="0.8" />
    <Image Width ="130" HorizontalAlignment="Center"
           Stretch="Uniform" Source="{Binding ImageThumbNail, Converter={StaticResource ThumbnailFileToImageSourceConverter}}" x:Phase="2"/>
    <TextBlock Grid.Row="1" MaxHeight="30" Text="{Binding Name}" x:Phase="1"
               Foreground="White" TextTrimming="CharacterEllipsis"/>
   </Grid> 

阅读本主题:
ListView 和 GridView UI 优化
ListView 和 GridView 数据虚拟化

于 2016-04-29T13:51:40.377 回答