[英]Compulsary requirement to take and store users photo using Xam.Plugin.Media
[英]How to add photo from gallery in Image with Xam.Plugin.Media?
我的 XAML 中有 Image 标签,源代码中有 Binding SelectedImage。 在 ViewModel 中,我希望有添加图像的逻辑,但使用我当前的代码,它不会插入照片。 进入画廊,但选择图像时不显示它。
这是我的实际代码:
主页.xaml:
<Image HeightRequest="50" Source="{Binding SelectedImage}" WidthRequest="50" />
视图模型.cs:
string selectedimage;
public string SelectedImage
{
get => selectedimage; set
{
selectedimage = value;
OnPropertyChanged();
}
}
async void insertImage()
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsPickPhotoSupported)
{
return;
}
var mediaOptions = new PickMediaOptions()
{
PhotoSize = PhotoSize.Medium
};
var selectedImageFile = await CrossMedia.Current.PickPhotoAsync(mediaOptions);
if (SelectedImage == null)
{
return;
}
SelectedImage = Convert.ToString(ImageSource.FromStream(() => selectedImageFile.GetStream()));
}
只需使用MediaFile
的Path
属性
// this code does not do anything useful
SelectedImage = Convert.ToString(ImageSource.FromStream(() => selectedImageFile.GetStream()));
// do this instead
SelectedImage = selectedImageFile.Path;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.