我们可以从 Phone (Android / iOS) 中选择一个图像并使用通用代码 (Xamarin Forms) 将其上传到服务器吗?
Can we pick a Image from Phone (Android / iOS) and upload it to server with common code(Xamarin Forms)?
要实现这一点,我应该使用特定于平台的代码,还是可以从 Xamarin.Forms.
的共享项目中的公共代码来实现?
基本上我需要打开图库并选择一张图片,然后将其以多部分字节数组的形式上传到服务器。
找不到任何使用 Xamarin Forms 的示例。
我假设您使用的是 PCL 解决方案。
有很多方法可以做到这一点。 Here 是我用来允许用户从图库中选择图像然后对该图像进行处理的插件。
虽然您也可以使用 XLabs 版本,但可用 here。
使用我推荐的插件,安装到PCL项目,Android项目,iOS项目后,可以这样写代码显示图片选择器,然后对该图像进行处理:
public List<string> Uris;
private ObservableCollection<MediaFile> _images;
public ObservableCollection<MediaFile> Images {
get { return _images ?? (_images = new ObservableCollection< MediaFile >()); }
set {
if(_images != value) {
_images = value;
OnPropertyChanged();
}
}
}
/// <summary>
/// Allows the user to pick a photo on their device using the native photo handlers and returns a stream, which we save to disk.
/// </summary>
/// <returns>string, the name of the image if everything went ok, 'false' if an exception was generated, and 'notfalse' if they simply canceled.</returns>
public async Task<string> PickPictureAsync() {
MediaFile file = null;
string filePath = string.Empty;
string imageName = string.Empty;
try {
file = await CrossMedia.Current.PickPhotoAsync();
#region Null Check
if(file == null) { return null; } //Not returning false here so we do not show an error if they simply hit cancel from the device's image picker
#endregion
imageName = "SomeImageName.jpg";
filePath = /* Add your own logic here for where to save the file */ //_fileHelper.CopyFile(file.Path, imageName);
} catch(Exception ex) {
Debug.WriteLine("\nIn PictureViewModel.PickPictureAsync() - Exception:\n{0}\n", ex); //TODO: Do something more useful
return null;
} finally { if(file != null) { file.Dispose(); } }
Receipts.Add(ImageSource.FromFile(filePath));
Uris.Add(filePath);
return imageName;
}
要实现这一点,我应该使用特定于平台的代码,还是可以从 Xamarin.Forms.
的共享项目中的公共代码来实现?基本上我需要打开图库并选择一张图片,然后将其以多部分字节数组的形式上传到服务器。
找不到任何使用 Xamarin Forms 的示例。
我假设您使用的是 PCL 解决方案。
有很多方法可以做到这一点。 Here 是我用来允许用户从图库中选择图像然后对该图像进行处理的插件。
虽然您也可以使用 XLabs 版本,但可用 here。
使用我推荐的插件,安装到PCL项目,Android项目,iOS项目后,可以这样写代码显示图片选择器,然后对该图像进行处理:
public List<string> Uris;
private ObservableCollection<MediaFile> _images;
public ObservableCollection<MediaFile> Images {
get { return _images ?? (_images = new ObservableCollection< MediaFile >()); }
set {
if(_images != value) {
_images = value;
OnPropertyChanged();
}
}
}
/// <summary>
/// Allows the user to pick a photo on their device using the native photo handlers and returns a stream, which we save to disk.
/// </summary>
/// <returns>string, the name of the image if everything went ok, 'false' if an exception was generated, and 'notfalse' if they simply canceled.</returns>
public async Task<string> PickPictureAsync() {
MediaFile file = null;
string filePath = string.Empty;
string imageName = string.Empty;
try {
file = await CrossMedia.Current.PickPhotoAsync();
#region Null Check
if(file == null) { return null; } //Not returning false here so we do not show an error if they simply hit cancel from the device's image picker
#endregion
imageName = "SomeImageName.jpg";
filePath = /* Add your own logic here for where to save the file */ //_fileHelper.CopyFile(file.Path, imageName);
} catch(Exception ex) {
Debug.WriteLine("\nIn PictureViewModel.PickPictureAsync() - Exception:\n{0}\n", ex); //TODO: Do something more useful
return null;
} finally { if(file != null) { file.Dispose(); } }
Receipts.Add(ImageSource.FromFile(filePath));
Uris.Add(filePath);
return imageName;
}