问题描述
我正在尝试从选择图片时获取基础图像数据.该功能本身带有摄像头和选择图片的功能非常完美,我只缺少一件事,那就是获取图像数据.我假设imagedata位于返回的mediaFile中,但是我不确定如何在contentPage上到达它.
I am trying to get the underlying image data from when you select a picture. The function itself with the cameraalbum and selecting a picture works perfectly and I am only missing one thing and that is to get the image data. The imagedata is inside the returned mediaFile i assume but I am not sure how I can reach it on my contentPage.
(我使用这个项目: https://github.com/XLabs/Xamarin-Forms-Labs/wiki/Camera )
想法是将图像数据发送到内容页面上的我的数据库中.
The idea is to send the imagedata to my database on the contentpage.
这是名为"CameraViewModel"的视图模型:
This is the viewmodel named "CameraViewModel":
public async Task SelectPicture()
{
Setup ();
ImageSource = null;
try
{
var mediaFile = await _Mediapicker.SelectPhotoAsync(new CameraMediaStorageOptions
{
DefaultCamera = CameraDevice.Front,
MaxPixelDimension = 400
});
VideoInfo = mediaFile.Path;
ImageSource = ImageSource.FromStream(() => mediaFile.Source);
}
catch (System.Exception ex)
{
Status = ex.Message;
}
}
现在在我的内容页面上,我有这个:
On my contentpage right now I have this:
CameraViewModel cameraOps = null;
public PhotoPage ()
{
InitializeComponent ();
cameraOps = new CameraViewModel ();
cameraOps. //trying to reach the mediafile here
}
更新的代码:
private MediaFile _file;
public MediaFile File
{
get { return _file; }
set
{
_file = value;
}
}
public async Task SelectPicture()
{
Setup ();
ImageSource = null;
try
{
File = await _Mediapicker.SelectPhotoAsync(new CameraMediaStorageOptions
{
DefaultCamera = CameraDevice.Front,
MaxPixelDimension = 400
});
VideoInfo = File.Path;
ImageSource = ImageSource.FromStream(() => File.Source);
}
catch (System.Exception ex)
{
Status = ex.Message;
}
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
File = value as MediaFile;
return ImageSource.FromStream(() => File.Source);
}
推荐答案
您可以在视图模型上创建属性:
You could create property on view model:
private MediaFile _file;
public MediaFile File
{
get { return _file; }
set
{
_file = value;
OnPropertyChanged();
}
}
然后将其绑定到ContentPage
:
<Image Source="{Binding File}, Converter={StaticResource MediaFileToImageSourceConverter}">
转换器:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var file = value as MediaFile;
return ImageSource.FromStream(() => file.GetStream()));
}
更新的代码:(不是mvvm的最佳传统,而是为您提供的示例)
Updated code: (not in best traditions of mvvm, but for your sample)
public async Task<MediaFile> SelectPicture()
{
Setup();
MediaFile file = null;
try
{
file = await _Mediapicker.SelectPhotoAsync(new CameraMediaStorageOptions
{
DefaultCamera = CameraDevice.Front,
MaxPixelDimension = 400
});
}
catch (System.Exception ex)
{
Status = ex.Message;
}
return file;
}
关于后面的代码:
public async void OnTakePhotoButtonClicked(object sender, EventArgs args)
{
var file = await cameraOps.SelectPicture();
someImage.ImageSource = ImageSource.FromStream(() => file.Source);
}
这篇关于如何到达我的视图模型上的mediaFile并在其内容页面上使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!