Xamarin使用Media.Plugin形成MVVM(Prism)-如何从设备存储中获取照片
我将Xamarin Forms PCL(MVVM Prism)与MediaPlugin()一起使用 我有一个名为ImagePath的带有公共属性的viewModelXamarin使用Media.Plugin形成MVVM(Prism)-如何从设备存储中获取照片,mvvm,xamarin.forms,prism,Mvvm,Xamarin.forms,Prism,我将Xamarin Forms PCL(MVVM Prism)与MediaPlugin()一起使用 我有一个名为ImagePath的带有公共属性的viewModel private string imagePath; public string ImagePath { get { return imagePath; } set { SetProperty(ref imagePath, value, "ImagePath"); } } 在我的XAML中,我有: <Image S
private string imagePath;
public string ImagePath
{
get { return imagePath; }
set { SetProperty(ref imagePath, value, "ImagePath"); }
}
在我的XAML中,我有:
<Image Source="{Binding ImagePath}" HeightRequest="100" WidthRequest="100"/>
var file.Path接收如下值:
“/storage/emulated/0/Android/data/{APPNAME}.Android/files/Pictures/{DIRECTORYNAME}/{IMAGENAME}”
使用此file.Path值,然后设置ImagePath属性:
ImagePath=file.Path
但拍摄的图像不会出现在屏幕上
我也尝试过使用
MediaPlugin文档中建议的Image.Source属性,但是我还没有达到我想要的结果
我还尝试了以下方法:
将公共属性转换为虚拟机:
private Image image = new Image();
public Image Image
{
get { return image; }
set { SetProperty(ref image, value, "Image"); }
}
Xaml:
有什么办法使它起作用吗?
谢谢 媒体插件返回一个媒体文件 我将其存储在一个用于存储和显示的类中
public class IncidentImage
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public int IncidentDetailsId { get; set; }
public byte[] ImageBytes { get; set; }
[Ignore]
public ImageSource ImageSource
{
get
{
ImageSource retval = null;
if (ImageBytes != null)
{
retval = ImageSource.FromStream(() => new MemoryStream(ImageBytes));
}
return retval;
}
}
}
像这样
var incidentImage = new IncidentImage
{
ImageBytes = image.ToByteArray()
};
图像是插件返回的媒体文件
然后我的XAML看起来像这样
<Image Source="{Binding IncidentImage.ImageSource}" Aspect="AspectFit" />
您也可以通过将ImageSource代码移动到转换器来实现这一点。嗨,Steve,我照您说的做,但是,我不显示我的照片第一期y有它的
image.ToByteArray()
我用那个代码来解决它
var memoryStream = new MemoryStream();
file.GetStream().CopyTo(memoryStream);
file.Dispose();
var incidentImage = new IncidentImage
{
ImageBytes = memoryStream.ToArray()
};`
太好了,成功了!!您是否有任何tib要将此ImageSource类型发布到sql数据库?或者保存文件并上传会更好?我将IncidentImage类型保存到本地Sqlite数据库,并通过REST API服务上传。您好,我也遇到了同样的问题,我只是想知道您是否仍然可以访问此解决方案并解决问题?下面的答案对我没有好处,我仍然看不到图片。所以可能我做错了。
var incidentImage = new IncidentImage
{
ImageBytes = image.ToByteArray()
};
<Image Source="{Binding IncidentImage.ImageSource}" Aspect="AspectFit" />
var memoryStream = new MemoryStream();
file.GetStream().CopyTo(memoryStream);
file.Dispose();
var incidentImage = new IncidentImage
{
ImageBytes = memoryStream.ToArray()
};`