xamarin.forms 位置访问和通讯录访问和拍照加图片,的操作(推荐)

来源:互联网 发布:vb.net 九九乘法表 编辑:程序博客网 时间:2024/06/05 17:11


联系人

访问通讯录(需要 READ_CONTACTS 权限 在Android上):

using Xamarin.Contacts;// ...var book = new AddressBook ();book.RequestPermission().ContinueWith (t => {    if (!t.Result) {        Console.WriteLine ("Permission denied by user or manifest");        return;    }    foreach (Contact contact in book.OrderBy (c => c.LastName)) {        Console.WriteLine ("{0} {1}", contact.FirstName, contact.LastName);    }}, TaskScheduler.FromCurrentSynchronizationContext());

地理位置

获得用户的位置(需要 ACCESS_COARSE_LOCATION 和 ACCESS_FINE_LOCATION 权限在Android上):

using Xamarin.Geolocation;// ...var locator = new Geolocator { DesiredAccuracy = 50 };//            new Geolocator (this) { ... }; on Androidlocator.GetPositionAsync (timeout: 10000).ContinueWith (t => {    Console.WriteLine ("Position Status: {0}", t.Result.Timestamp);    Console.WriteLine ("Position Latitude: {0}", t.Result.Latitude);    Console.WriteLine ("Position Longitude: {0}", t.Result.Longitude);}, TaskScheduler.FromCurrentSynchronizationContext());

注意:在iOS 8.0 +必须设置 NSLocationWhenInUseUsageDescription 或 NSLocationAlwaysUsageDescription在你的 Info.plist 文件以便Xamarin的。 移动将从用户请求适当的权限。

媒体

mediapicker 允许您调用本机UI或选择的照片或视频。 鉴于 这个UI交互,代码(虽然比手工做简单)不会 完全跨平台的。

在iOS拍照,Windows Phone或WinRT:

using Xamarin.Media;// ...var picker = new MediaPicker();picker.PickPhotoAsync().ContinueWith (t => {    MediaFile file = t.Result;    Console.WriteLine (file.Path);}, TaskScheduler.FromCurrentSynchronizationContext());

在Android和iOS上可选,你控制UI。

拍照在Android上(需要 WRITE_EXTERNAL_STORAGE 权限):

using Xamarin.Media;// ...protected override void OnCreate (Bundle bundle){    var picker = new MediaPicker (this);    if (!picker.IsCameraAvailable)        Console.WriteLine ("No camera!");    else {        var intent = picker.GetTakePhotoUI (new StoreCameraMediaOptions {            Name = "test.jpg",            Directory = "MediaPickerSample"        });        StartActivityForResult (intent, 1);    }}protected override void OnActivityResult (int requestCode, Result resultCode, Intent data){    // User canceled    if (resultCode == Result.Canceled)        return;    data.GetMediaFileExtraAsync (this).ContinueWith (t => {        Console.WriteLine (t.Result.Path);    }, TaskScheduler.FromCurrentSynchronizationContext());}

拍照在iOS控制界面:

using Xamarin.Media;// ...var picker = new MediaPicker();MediaPickerController controller = picker.GetTakePhotoUI (new StoreCameraMediaOptions {    Name = "test.jpg",    Directory = "MediaPickerSample"});// On iPad, you'll use UIPopoverController to present the controllerPresentViewController (controller, true, null);controller.GetResultAsync().ContinueWith (t => {    // Dismiss the UI yourself    controller.DismissViewController (true, () => {        MediaFile file = t.Result;    });}, TaskScheduler.FromCurrentSynchronizationContext());
注意到iOS 8开发者

显示一个 mediapicker 在回答 UIActionSheet.Clicked 在iOS 8日事件会导致意想不到的行为。 应用程序应该有条件地使用一个更新 UIAlertController 的风格 UIAlertControllerStyle.ActionSheet。 更多信息见iOS样本。



dll下载地址http://components.xamarin.com/view/xamarin.mobile

  1. 开发交流qq470138890

    另外推荐168vpn,很多开发更新时候必备,



1 0
原创粉丝点击