Xamarin-显示来自base64字符串的图像 [英] Xamarin - Show image from base64 string

查看:72
本文介绍了Xamarin-显示来自base64字符串的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Xamarin和XAML东西还很陌生,这是迄今为止我在Android& iPhone(仅使用Android):

I'm pretty new to Xamarin and XAML stuff and here is what I've done so far in my portable project used by Android & iPhone (only using Android):

Item.cs(从JSON加载)

Item.cs (loaded from 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; }
    }

ItemsView.xaml:

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>

我正确显示了标签,但图像没有显示。
有人可以解释我在做什么吗?

I correctly get my labels displayed but the image isn't. Can someone explain me what I'm doing wrong?

推荐答案

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

The type of your Image property should be ImageSource, not Image, as you apparently want to bind an ImageCell's ImageSource property. Besides that, calling OnPropertyChanged in a property getter never works, because the PropertyChanged event has to be fired before a binding (or any other consumer) can get a changed property value.

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

Instead of Image.Source="{Binding ...}, the correct binding would have to be

<ImageCell ... ImageSource="{Binding Path=Image}" />

属性应这样声明:

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");
    }
}

如果您真的需要延迟创建 Image 属性值,您可以将其设置为只读,并在其中调用相应的 OnPropertyChanged 调用 ImageBase64 设置器:

If you really need lazy creation of the Image property value, you could make it read-only, and make the corresponding OnPropertyChanged call in the ImageBase64 setter:

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;
    }
}

这篇关于Xamarin-显示来自base64字符串的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆