UWP App:`Image.SetSource`在`KnownPlaces`之外的`StorageFiles`上挂起计算机

问题描述 投票:-1回答:1

这个很难解释,所以我给你一些实际和伪代码:

try
{
   // If source (a string) points towards a file that is available with 
   // StorageFile.GetFileFromPathAsync(), just open the file that way.
   // If that is not possible, use the path to look up an Access Token
   // and use the file from the StorageFolder gotten via that token.

   StorageFile file = await GetFileFromAccessList(source);

   if (file != null)
   {
      bitmap = new BitmapImage();
      using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read))
      {
          await bitmap.SetSourceAsync(fileStream);
      }
   }
}
catch (Exception e)
{
   string s = e.Message;
   bitmap = null;
}

使用以下方法:

    public async Task<StorageFile> GetFileFromAccessList(string path)
    {
        StorageFile result = null;

        if (String.IsNullOrEmpty(path) == false)
            try
            {
                // Try to access to file directly...
                result = await StorageFile.GetFileFromPathAsync(path);

            }
            catch (Exception)
            {
                result = null;
                try
                {
                    // See if the folder this thing is in is in the access list...
                    StorageFolder folder = await GetFolderFromAccessList(Path.GetFullPath(path));

                    // If there is a folder, try that.
                    if (folder != null)
                        result = await folder.GetFileAsync(Path.GetFileName(path));
                }
                catch (Exception)
                {
                    result = null;
                }
            }

        return result;
    }

生成的位图在Image.SetSource()中用作ImageSource

现在是什么让我失望:对于存储在apps文件夹或KnownFolders中的文件,此调用可以完美,快速且坚如磐石。因此,当我不需要访问令牌时,它就像一个魅力。 Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFolderAsync(token)

但是,如果我必须使用访问令牌,它会中断,而不是所有时间此代码不会立即中断:当我尝试同时打开5-7个源文件时,它会中断。重复一遍:如果我显示5-7张图像,这是有效的。如果我尝试打开更多,它会冻结PC。当我打开没有令牌的StorageFiles时,不会出现这样的问题。

我可以使用普通文件操作访问这些文件。我可以从它们创建位图,处理它们,工作。我不能让他们成为XAML Image的来源。

有什么想法吗?

xaml uwp bitmapimage
1个回答
0
投票

啊清晰度。事实证明,使用DataContextChanged事件通过Image.SetSource()刷新位图是谋杀武器。

解决方案:声明类型为BitmapSource的属性。将Image.Source绑定到该财产。在Image.LoadedImage.DataContextChanged上使用加载的位图更新属性。在我能够测试的所有条件下,现在可以稳定快速地工作。

© www.soinside.com 2019 - 2024. All rights reserved.