如何对图像进行数据绑定?
How can I databind an image?
如何使用 XAML 对图像进行数据绑定?
具体来说,我有一个要转换为图像的流对象。
但是,我找不到如何执行此操作的基本示例。
1.视图模型 属性 是什么样子的?
我使用 ImageSource 吗?
我需要值转换器吗?
2。 XAML 是什么样子的?
Forms 有一个 StreamImageSource 您可以用来执行此操作。但是,我认为如果不编写自定义转换器就不能在 XAML 中使用它。要在代码中使用它,您可以这样做:
image1.Source = ImageSource.FromStream(() =>
{
// whatever you need to do to create your stream
return stream;
});
该演示通过代码说明了绑定。对于 XAML
实施,您需要以下内容:-
<Image Source="{Binding MyImageAsBytes, Converter={StaticResource MyByteToImageSourceConverter}}" />
在 ViewModel
中包含 byte[]
后,您 将 需要一个转换器来将这个包含图像的字节数组转换为Xamarin.Forms
ImageSource
.
转换器获取 byte[]
数组并通过以下方式转换为 ImageSource
:-
objImageSource = ImageSource.FromStream(() => new MemoryStream(bytImageData));
例子:-
StackLayout objStackLayout = new StackLayout();
byte[] bytImage = { your image as a byte collection }
this.BindingContext = new MyImageViewModel()
{
MyImageAsBytes = bytImage
};
Image objImage = new Image();
objImage.SetBinding(Image.SourceProperty, "MyImageAsBytes", converter: new MyByteToImageSourceConverter());
objStackLayout.Children.Add(objImage);
ViewModel:-
public class MyImageViewModel
: Xamarin.Forms.View
{
public static readonly BindableProperty MyImageAsBytesProperty = BindableProperty.Create<MyImageViewModel, byte[]>(p => p.MyImageAsBytes, default(byte[]));
public byte[] MyImageAsBytes
{
get { return (byte[])GetValue(MyImageAsBytesProperty); }
set { SetValue(MyImageAsBytesProperty, value); }
}
}
转换器:-
public class MyByteToImageSourceConverter
: IValueConverter
{
public object Convert(object pobjValue, Type pobjTargetType, object pobjParameter, System.Globalization.CultureInfo pobjCulture)
{
ImageSource objImageSource;
//
if (pobjValue != null)
{
byte[] bytImageData = (byte[])pobjValue;
//
objImageSource = ImageSource.FromStream(() => new MemoryStream(bytImageData));
}
else
{
objImageSource = null;
}
//
return objImageSource;
}
public object ConvertBack(object pobjValue, Type pobjTargetType, object pobjParameter, System.Globalization.CultureInfo pobjCulture)
{
throw new NotImplementedException();
}
}
如何使用 XAML 对图像进行数据绑定?
具体来说,我有一个要转换为图像的流对象。
但是,我找不到如何执行此操作的基本示例。
1.视图模型 属性 是什么样子的?
我使用 ImageSource 吗?
我需要值转换器吗?
2。 XAML 是什么样子的?
Forms 有一个 StreamImageSource 您可以用来执行此操作。但是,我认为如果不编写自定义转换器就不能在 XAML 中使用它。要在代码中使用它,您可以这样做:
image1.Source = ImageSource.FromStream(() =>
{
// whatever you need to do to create your stream
return stream;
});
该演示通过代码说明了绑定。对于 XAML
实施,您需要以下内容:-
<Image Source="{Binding MyImageAsBytes, Converter={StaticResource MyByteToImageSourceConverter}}" />
在 ViewModel
中包含 byte[]
后,您 将 需要一个转换器来将这个包含图像的字节数组转换为Xamarin.Forms
ImageSource
.
转换器获取 byte[]
数组并通过以下方式转换为 ImageSource
:-
objImageSource = ImageSource.FromStream(() => new MemoryStream(bytImageData));
例子:-
StackLayout objStackLayout = new StackLayout();
byte[] bytImage = { your image as a byte collection }
this.BindingContext = new MyImageViewModel()
{
MyImageAsBytes = bytImage
};
Image objImage = new Image();
objImage.SetBinding(Image.SourceProperty, "MyImageAsBytes", converter: new MyByteToImageSourceConverter());
objStackLayout.Children.Add(objImage);
ViewModel:-
public class MyImageViewModel
: Xamarin.Forms.View
{
public static readonly BindableProperty MyImageAsBytesProperty = BindableProperty.Create<MyImageViewModel, byte[]>(p => p.MyImageAsBytes, default(byte[]));
public byte[] MyImageAsBytes
{
get { return (byte[])GetValue(MyImageAsBytesProperty); }
set { SetValue(MyImageAsBytesProperty, value); }
}
}
转换器:-
public class MyByteToImageSourceConverter
: IValueConverter
{
public object Convert(object pobjValue, Type pobjTargetType, object pobjParameter, System.Globalization.CultureInfo pobjCulture)
{
ImageSource objImageSource;
//
if (pobjValue != null)
{
byte[] bytImageData = (byte[])pobjValue;
//
objImageSource = ImageSource.FromStream(() => new MemoryStream(bytImageData));
}
else
{
objImageSource = null;
}
//
return objImageSource;
}
public object ConvertBack(object pobjValue, Type pobjTargetType, object pobjParameter, System.Globalization.CultureInfo pobjCulture)
{
throw new NotImplementedException();
}
}