简体   繁体   English

UWP:当没有可用的 Inte.net 时设置 ImageSource 时出现问题

[英]UWP: Issues with setting ImageSource when no Internet is available

I have a scenario where I have UI which has a dropdown and an image.我有一个场景,我有一个 UI,它有一个下拉菜单和一个图像。 This UI shows the first item(Item A) in the dropdown selected which shows Image A on the UI.此 UI 显示所选下拉列表中的第一项(项目 A),它在 UI 上显示图像 A。 When this UI shows up first the inte.net is disconnected.当此 UI 首次出现时,inter.net 已断开连接。 I still see Image A. Now I try to select another item from the dropdown Item B and this time I don't see Image B.我仍然看到图像 A。现在我尝试 select 从下拉项目 B 中选择另一个项目,但这次我没有看到图像 B。

This is how I set the Image Source这就是我设置图像源的方式

UI:用户界面:

<Image source= "{x:bind Viewmodel.SampleImage}, mode=OneWay">
</Image>

Backend:后端:

private ImageSource sampleImage;
public ImageSource SampleImage
{
   get => this.sampleImage;
   set => this.SetProperty(ref this.sampleImage, value);
 }

public void Initialize()
{
   // Everytime I change my selection from the dropdown this function is triggered which tries to set the value of Image displayed in the UI
    BitmapImage fallbackImage = new BitmapImage(new Uri("https://randomImage"));
    this.SampleImage = fallbackImage;
 }

I need help in understanding why the second image Image B does not show up when the inte.net is disconnected here.And how do I handle this case when the inte.net is disconnected and I need to still display the image if I don't have local fallback images.我需要帮助来理解为什么当 inte.net 在这里断开连接时第二个图像图像 B 不显示。当 inte.net 断开连接时我如何处理这种情况,如果我不这样做,我仍然需要显示图像没有本地后备图像。

Based on the code you share, the Initialize method will only be triggered when the dropdown selection is changed, so the image is actually downloaded only when the item is selected.根据您共享的代码, Initialize方法只会在下拉选择更改时触发,因此只有在选择项目时才会实际下载图像。

When you disconnect from the inte.net, the download action will fail.当您断开与 inte.net 的连接时,下载操作将失败。 The image B will not be show because the image is not downloaded. image B不会显示,因为图片未下载。 The reason why image A still should is that the BitmapImage will cache the image it uses before. image A仍然应该的原因是BitmapImage会缓存它之前使用的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM