赞
踩
要实现这个目标,我应该选择特定于平台的代码,还是可以从共享的Xamarin.Forms项目中的常用代码中实现.
基本上我需要打开Gallery并选择一个图像,然后将其上传到多部分字节数组中的服务器.
找不到使用Xamarin Forms的任何示例.
我假设您正在使用PCL解决方案.
有很多方法可以做到这一点.这是我用来允许用户从图库中选择图像然后对该图像执行某些操作的插件.
虽然你也可以使用XLabs版本,可以在这里找到.
使用我建议的插件,在将其安装到PCL项目,Android项目和iOS项目之后,您可以编写这样的代码来显示图像选择器,然后对该图像执行某些操作:
public ListUris; private ObservableCollection _images; public ObservableCollection Images { get { return _images ?? (_images = new ObservableCollection< MediaFile >()); } set { if(_images != value) { _images = value; OnPropertyChanged(); } } } /// /// Allows the user to pick a photo on their device using the native photo handlers and returns a stream, which we save to disk. /// ///string, the name of the image if everything went ok, 'false' if an exception was generated, and 'notfalse' if they simply canceled. public async TaskPickPictureAsync() { 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; }
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。