我有一个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/