【问题标题】:Image doesnt load on Xaml图像未在 Xaml 上加载
【发布时间】:2016-10-26 07:28:49
【问题描述】:

我在加载图像时遇到问题(仅在这台电脑上,在另一台上没问题)

我尝试了几种放置图像的方法

 <Image 
        MinWidth="190"
        Stretch="Fill"
        MinHeight="190"    
        Source="{Binding ImagePath, Converter={StaticResource UriToImageConverter}}">
        <!--If you use my way in answer you need replace Source like this-->
        Source="{Binding Image}"><!--Bitmapimage property in model-->
 </Image>

它只有在我放置像“ms-appx”这样的路径时才有效,但在目标中我需要使用“绑定”它在其他电脑上的工作没有任何麻烦,但这里我在页面上有“透明”图片

【问题讨论】:

  • 用户选择图片,我把 'StorageFile.Path' 放到 'ImagePath'
  • 你应该阅读图像文件并使用BitmpaImage作为图像的来源。
  • @tao 我有返回位图的转换器return new BitmapImage(new Uri(stringUri, UriKind.RelativeOrAbsolute));
  • @SmiLe 你应该使用SetSourceAsync 方法。

标签: c# xaml uwp win-universal-app windows-10-universal


【解决方案1】:

好的,我只是把图像放到StorageFile's 并复制到本地文件夹,然后从那里取出并放入我的Model's 属性中

之前:

 this.collageimages = await OpenPicker.PickMultipleFilesAsync();
 for (var i = 0; i < this.collageimages.Count; i++)
{
  var stream = await collageimages[i].OpenAsync(FileAccessMode.Read);
  this.image = new BitmapImage();
  await this.image.SetSourceAsync(stream);
}

之后:

this.collageimages = await OpenPicker.PickMultipleFilesAsync();
 for (var i = 0; i < this.collageimages.Count; i++)
{
  await this.collageimages[i].CopyAsync(ApplicationData.Current.LocalFolder, "collageImage_"+i+"", NameCollisionOption.ReplaceExisting);
  var tempStorage = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appdata:///local/collageImage_"+i+""));
  var stream = await tempStorage.OpenAsync(FileAccessMode.Read);
  this.image = new BitmapImage();
  await this.image.SetSourceAsync(stream);
}

我不声称是最好的答案,如果你知道更好的方法,请写出来

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-24
相关资源
最近更新 更多