gpt4 book ai didi

c# - 如何使用MVVM从Restful Service向Xamarin.Forms View 显示图像

转载 作者:行者123 更新时间:2023-12-03 10:31:00 25 4
gpt4 key购买 nike

我有一个Restful Service,可以为我提供图像流。当我在浏览器中调用该服务以显示图像时,它也可以正常工作。
现在,我试图在Xamarin View 中显示该图像,如下所示:

<ffimageloading:CachedImage Source="{Binding ImageSource}" Aspect="AspectFill" WidthRequest="75" HeightRequest="200"></ffimageloading:CachedImage>
ViewModel包含以下功能:
private ImageSource _imageSource;
public ImageSource ImageSource
{
get { return _imageSource; }
set
{
_imageSource = value;
SetProperty(ref _imageSource, value);
}
}
private string itemId;
public string ItemId
{
get
{
return itemId;
}
set
{
itemId = value;
LoadItemId(value);
}
}

public async void LoadItemId(string itemId)
{
try
{
var item = await MockDataStore.GetItemAsync(itemId);
Id = item.ID;
Text = item.TITLE;
Description = item.CONTENT;
//ImageSource = "http://127.0.0.1:4321/Service.svc/GetImage/?uri=12";
ImageSource = "https://www.shareicon.net/data/128x128/2016/05/02/758874_package_512x512.png";
}
catch (Exception)
{
Debug.WriteLine("Failed to Load Item");
}
}
当我直接在 View 的图像源中提供图像链接时,它可以完美工作。
<ffimageloading:CachedImage Source="https://www.shareicon.net/data/128x128/2016/05/02/758874_package_512x512.png" Aspect="AspectFill" WidthRequest="75" HeightRequest="200"></ffimageloading:CachedImage>
但是,当我尝试从ViewModel获取ImageSource时,没有任何 View 在服务调用或任何图像链接上都显示图像。
我在调试时看到的奇怪现象是,每当我更改 View 中的任何内容以激活热重载时,它都会突然在模拟器中显示来自ViewModel的图像。
我是Xamarin的新手,不知道如何解决它。

最佳答案

您的VM没有实现INotifyPropertyChanged,将其放置到位并向 View /xaml发出对ImageSource的更改的信号。

关于c# - 如何使用MVVM从Restful Service向Xamarin.Forms View 显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66247194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com