C# Xamarin-显示base64字符串中的图像

C# Xamarin-显示base64字符串中的图像,c#,wpf,xaml,xamarin,xamarin.ios,C#,Wpf,Xaml,Xamarin,Xamarin.ios,我对Xamarin和XAML的东西还很陌生,以下是我在Android和iPhone使用的便携式项目(仅使用Android)中迄今为止所做的工作: Item.cs(从JSON加载) ItemsView.xaml: <StackLayout VerticalOptions="FillAndExpand" Padding="5,20,5,0" > <Label Text="Items" VerticalOptions="Center" Font="35" HorizontalOp

我对Xamarin和XAML的东西还很陌生,以下是我在Android和iPhone使用的便携式项目(仅使用Android)中迄今为止所做的工作:

Item.cs(从JSON加载)

ItemsView.xaml:

<StackLayout VerticalOptions="FillAndExpand" Padding="5,20,5,0" >
  <Label Text="Items" VerticalOptions="Center" Font="35" HorizontalOptions="Center" />
  <ListView x:Name="list" ItemsSource="{Binding Items}">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ImageCell
                        Text="{Binding ItemName}"
                        Detail="{Binding Infos, StringFormat='{0}'}"
          Image.Source="{Binding Path=Image}">
        </ImageCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>
</StackLayout>

我正确地显示了标签,但图像没有显示。
有人能解释一下我做错了什么吗?

您的
图像
属性的类型应该是
图像源
,而不是
图像
,因为您显然想绑定ImageCell的
图像源
属性。除此之外,在属性getter中调用
OnPropertyChanged
是行不通的,因为必须在绑定(或任何其他使用者)获得更改的属性值之前触发
PropertyChanged
事件

必须使用正确的绑定,而不是
Image.Source=“{Binding…}

<ImageCell ... ImageSource="{Binding Path=Image}" />
如果确实需要延迟创建
图像
属性值,可以将其设置为只读,并在
ImageBase64
setter中进行相应的
OnPropertyChanged
调用:

private string imageBase64
public string ImageBase64
{
    get { return imageBase64; }
    set
    {
        imageBase64 = value;
        OnPropertyChanged("ImageBase64");
        OnPropertyChanged("Image");
    } 
}

private Xamarin.Forms.ImageSource image;
public Xamarin.Forms.ImageSource Image
{
    get
    {
        if (image == null)
        {
            image = Xamarin.Forms.ImageSource.FromStream(
                () => new MemoryStream(Convert.FromBase64String(ImageBase64)));
        }
        return image;
    }
}
我突然想到两件事:a)您正在将ImageSource绑定到图像b)“Path=”是不必要的
private string imageBase64;
public string ImageBase64
{
    get { return imageBase64; }
    set
    {
        imageBase64 = value;
        OnPropertyChanged("ImageBase64");

        Image = Xamarin.Forms.ImageSource.FromStream(
            () => new MemoryStream(Convert.FromBase64String(imageBase64)));
    } 
}

private Xamarin.Forms.ImageSource image;
public Xamarin.Forms.ImageSource Image
{
    get { return image; }
    set
    {
        image = value;
        OnPropertyChanged("Image");
    }
}
private string imageBase64
public string ImageBase64
{
    get { return imageBase64; }
    set
    {
        imageBase64 = value;
        OnPropertyChanged("ImageBase64");
        OnPropertyChanged("Image");
    } 
}

private Xamarin.Forms.ImageSource image;
public Xamarin.Forms.ImageSource Image
{
    get
    {
        if (image == null)
        {
            image = Xamarin.Forms.ImageSource.FromStream(
                () => new MemoryStream(Convert.FromBase64String(ImageBase64)));
        }
        return image;
    }
}