XAML Xamarin -显示来自base64字符串的图像

axr492tv  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(211)

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

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>

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

vawmfj5a

vawmfj5a1#

Image属性的类型应该是ImageSource,而不是Image,因为你显然想绑定ImageCell的ImageSource属性。除此之外,在属性getter中调用OnPropertyChanged永远不会起作用,因为PropertyChanged事件必须在绑定(或任何其他消费者)获得更改的属性值 * 之前 * 被触发。
而不是Image.Source="{Binding ...},正确的绑定必须是

<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属性值,可以将其设置为只读,并在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;
    }
}
xkrw2x1b

xkrw2x1b2#

您也可以构造at字符串的形式

image.Source = $"data:image/png;base64, {base64Image}";

相关问题