gpt4 book ai didi

c# - 如何在我的 View 模型上访问 mediaFile 并在我的内容页面上使用它?

转载 作者:太空宇宙 更新时间:2023-11-03 23:13:52 24 4
gpt4 key购买 nike

我正在尝试从您选择图片时获取基础图像数据。相机相册和选择图片的功能本身完美无缺,我只缺少一件事,那就是获取图像数据。图像数据在我假设返回的 mediaFile 中,但我不确定如何在我的 contentPage 上访问它。

(我用这个项目:https://github.com/XLabs/Xamarin-Forms-Labs/wiki/Camera)

我的想法是将图像数据发送到我在内容页面上的数据库。

这是名为“CameraViewModel”的 View 模型:

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;

}
}

现在我的内容页面上有这个:

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

最佳答案

您可以在 View 模型上创建属性:

    private MediaFile _file;
public MediaFile File
{
get { return _file; }
set
{
_file = value;
OnPropertyChanged();
}
}

然后绑定(bind)到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 的最佳传统,而是您的示例)

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

关于c# - 如何在我的 View 模型上访问 mediaFile 并在我的内容页面上使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37946391/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com