Xamarin 使用 Media.Plugin 形成 MVVM (Prism) - 如何从设备存储中获取拍摄的照片 [英] Xamarin Forms MVVM (Prism) with Media.Plugin - How to get a taken picture from device storage
问题描述
我正在使用 Xamarin Forms PCL (MVVM Prism) 和 MediaPlugin (https://github.com/jamesmontemagno/MediaPlugin).
我有一个名为 ImagePath 的公共属性的 viewModel
I have a viewModel with a public property called ImagePath
private string imagePath;
public string ImagePath
{
get { return imagePath; }
set { SetProperty(ref imagePath, value, "ImagePath"); }
}
在我的 XAML 中,我有:
in my XAML i have:
<Image Source="{Binding ImagePath}" HeightRequest="100" WidthRequest="100"/>
当我将图像 url(来自 Web)设置为 ImagePath 属性时,它可以正常工作.(我正在设置这样的字符串:http://www.myaddress.com/myimage.jpg")
When i set a image url (from web) to ImagePath property, it works without problems. (I am setting an string like this: "http://www.myaddress.com/myimage.jpg")
但是,当我使用以下代码(MediaPlugin)拍照时:
But, when I take a picture using the following code (MediaPlugin):
var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
Directory = "Sample",
Name = "test.jpg"
});
ImagePath = file.Path;
// also tried: ImagePath = ImageSource.FromFile(file.Path).toString();
var file.Path 接收这样的值:
the var file.Path receives a value like this:
"/storage/emulated/0/Android/data/{APPNAME}.Android/files/Pictures/{DIRECTORYNAME}/{IMAGENAME}"
"/storage/emulated/0/Android/data/{APPNAME}.Android/files/Pictures/{DIRECTORYNAME}/{IMAGENAME}"
使用这个 file.Path 值然后我设置 ImagePath 属性:ImagePath = file.Path;
With this file.Path value then i'm setting ImagePath property: ImagePath = file.Path;
但是拍摄的图像没有出现在屏幕上.
But the taken image does not appear on the screen.
我也曾尝试使用MediaPlugin 文档中建议的 Image.Source 属性,但是我还没有达到我想要的结果.
I also have tried to use the Image.Source property as suggested in MediaPlugin docs, however i have not reached the result i want.
我也尝试了以下方法:
公共属性进入虚拟机:
private Image image = new Image();
public Image Image
{
get { return image; }
set { SetProperty(ref image, value, "Image"); }
}
XML:
<Image Source="{Binding Image.Source}" HeightRequest="100" WidthRequest="100"/>
设置文件值后(如前所示):
After set file value (as showed before):
image.Source = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
file.Dispose();
return stream;
});
有什么办法让它工作吗?谢谢!!
Is there any way to make it work? Thanks!!
推荐答案
媒体插件返回一个 MediaFile.
The Media Plugin returns a MediaFile.
我将它存储在一个用于存储和显示的类中
I store this in a class I use for storage and display
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()
};
image 是插件返回的 MediaFile.
image being the MediaFile returned by the plugin.
然后我的 XAML 看起来像这样
Then my XAML looks like this
<Image Source="{Binding IncidentImage.ImageSource}" Aspect="AspectFit" />
您也可以通过将 ImageSource 代码移动到转换器来实现.
You could also do it by moving the ImageSource code to a converter.
这篇关于Xamarin 使用 Media.Plugin 形成 MVVM (Prism) - 如何从设备存储中获取拍摄的照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!