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

标签 c# rest xamarin mvvm service

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

相关文章:

c# - 正则表达式:如何接受符号 (@)?

c# - 如何在 Xamarin 中压缩来自 PCL 的视频

java - 是否可以在没有 @DirtiesContext 的情况下测试 Spring REST Controller ?

ios - REST API、Swift、自动更新

c# - 无法正确部署应用程序 Xamarin

c# - 如何在 Xamarin.Forms ListView 下添加按钮

c# - 如何重命名 TreeView 中的节点?

c# - task.Status == RanToCompletion 和 task.IsCompleted 的比较

c# - 最小起订量验证某些不可预测的值

java - MultipartFile 在 RestTemplate 中失败,错误请求 400 无正文