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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37080258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:47:52  来源:igfitidea点击:

Xamarin - Show image from base64 string

c#wpfxamlxamarinxamarin.ios

提问by Arnaud F.

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):

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

Item.cs (loaded from JSON)

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:

项目视图.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?

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

回答by Clemens

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

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

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

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

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

The properties should be declared like this:

属性应该这样声明:

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

If you reallyneed lazy creation of the Imageproperty value, you could make it read-only, and make the corresponding OnPropertyChangedcall in the ImageBase64setter:

如果你真的需要懒惰的创建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;
    }
}