Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Windows RT应用程序中预加载图像_C#_Vb.net_Windows Store Apps - Fatal编程技术网

C# 在Windows RT应用程序中预加载图像

C# 在Windows RT应用程序中预加载图像,c#,vb.net,windows-store-apps,C#,Vb.net,Windows Store Apps,我有一个在Windows RT上运行的Windows应用商店应用程序,其中包括一个图像列表框。有相当多的,所以当他们第一次加载时,绘画过程是非常明显的。我知道有一个缓存机制在幕后运行,我想用它来预加载列表顶部的前几个图像,以便列表立即显示 有办法吗?我找到了解决办法 IRandomAccessStream stream = await storageFile.OpenAsync(FileAccessMode.Read); BitmapImage bitmapImage = new BitmapI

我有一个在Windows RT上运行的Windows应用商店应用程序,其中包括一个图像列表框。有相当多的,所以当他们第一次加载时,绘画过程是非常明显的。我知道有一个缓存机制在幕后运行,我想用它来预加载列表顶部的前几个图像,以便列表立即显示

有办法吗?

我找到了解决办法

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

这样就不会闪烁。

让它工作了,下面是我是如何做到的。我创建了一个
字典
来存储预编码的图像,并在appstart上加载了它。还创建了一个函数,该函数采用路径并从字典中返回预加载的图像(如果存在),或正常加载(如果不存在)

private Dictionary<string, BitmapImage> preload;
.
.
.


public void PreloadImages(BaseUri, path)
{
    if (!preload.ContainsKey(path))
    {
        var uri = new Uri(BaseUri, path);
        var file = await StorageFile.GetFileFromApplicationUriAsync(uri);

        IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
        BitmapImage bmp = new BitmapImage();
        if (!preload.ContainsKey(path))
            preload.Add(path, bmp);
    }
}
public BitmapImage ImageFromRelativePath(Uri BaseUri, string path)
{
    if (preload.ContainsKey(path))
    {
        return preload[path];
    }
    else
    {
        var uri = new Uri(BaseUri, path);
        BitmapImage bmp = new BitmapImage();
        bmp.UriSource = uri;
        return bmp;
    }
}
以及正在运行的转换器:

<Image Source="{Binding Path=ImagePath, Mode=OneWay, Converter={StaticResource CachedImageConverter}}"></Image>

不幸的是,代码原样在WinRT上不起作用,但它确实为我指明了正确的方向。所以+1!
<Image Source="{Binding Path=ImagePath, Mode=OneWay, Converter={StaticResource CachedImageConverter}}"></Image>