Xamarin - 从base64字符串显示图像

Arn*_* F. 10 c# wpf xaml xamarin.ios xamarin

我是Xamarin和XAML的新手,这是我迄今为止在Android和iPhone使用的便携式项目中所做的事情(仅使用Android):

Item.cs(从JSON加载)

    [JsonProperty("image")]
    private string ImageBase64 { get; set; }

    [JsonIgnore]
    private Xamarin.Forms.Image _image = null;

    [JsonIgnore]
    public Xamarin.Forms.Image Image
    {
        get
        {
            if (_image == null)
            {
                _image = new Xamarin.Forms.Image()
                {
                    Source = Xamarin.Forms.ImageSource.FromStream(() => new MemoryStream(Convert.FromBase64String(ImageBase64))),
                    BackgroundColor = Color.White,
                    WidthRequest = 64,
                    HeightRequest = 64,
                };
                OnPropertyChanged("Image");
            }
            return _image;
        }
        private set
        { _image = value; }
    }
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

我正确显示我的标签,但图像不是.有人可以解释一下我做错了什么吗?

Cle*_*ens 15

您的Image属性的类型应该是ImageSource,Image因为您显然想要绑定ImageCell的ImageSource属性.除此之外,调用OnPropertyChanged属性getter永远不会起作用,因为PropertyChanged必须绑定(或任何其他使用者)获得更改的属性值之前触发事件.

而不是Image.Source="{Binding ...},正确的绑定必须是

<ImageCell ... ImageSource="{Binding Path=Image}" />
Run Code Online (Sandbox Code Playgroud)

应该像这样声明属性:

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您确实需要延迟创建Image属性值,可以将其设置为只读,并OnPropertyChangedImageBase64setter中进行相应的调用:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)