我们可以从手机(Android / iOS)中select一个图像,并使用通用代码(Xamarin Forms)将其上传到服务器?

要实现这一点,我应该去特定于平台的代码,或者我可以从Xamarin.Forms的共享项目中的通用代码做到这一点。

基本上我需要打开Gallery并select一个图像,然后上传到服务器的多部分字节数组。

找不到任何使用Xamarin Forms的例子。

我假设你正在使用PCL解决scheme。

有很多方法可以做到这一点。 这里是我用来允许用户从图库中select图像的插件,然后用该图像做一些事情。

尽pipe你也可以使用XLabs版本,可以在这里find 。

使用我build议的插件,在将其安装到PCL项目,Android项目和iOS项目之后,您可以编写这样的代码来显示图像select器,然后对该图像执行一些操作:

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