Device APIs with Xamarin http://schoolacademy.telerik.com Device APIs with Xamarin Accessing Phonebook, Location, Camera http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/databases/ Xamarin apps for iOS, Android & WinPhone Telerik School Academy http://schoolacademy.telerik.com
Table of Contents Reference Xamarin.Mobile Accessing Device APIs * Table of Contents Reference Xamarin.Mobile Accessing Device APIs Geo location Address book Camera (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
Xamarin.Mobile Download Xamarin.Mobile or install through Components for each platform
Table of Contents Geo location Reference Xamarin.Mobile * Table of Contents Reference Xamarin.Mobile Accessing Device APIs Geo location Address book Camera (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
Geolocation Shared Code Add interface IGeolocator Use DependencyServices to call methods Use MessageingCenter to Subscribe to event Public interface IGeolocator { void GetPosition(); } IGeolocator locator = DependencyService .Get<IGeolocator>(); locator.GetPosition(); MessagingCenter.Subscribe<IGeolocator, AppPosition>(this, [matching string], DisplayPosition);
Geolocation Implementation Add device implementation and use Dependency [assembly: Dependency (typeof (Geolocator_Android))] public class Geolocator_Android : Action, IGeolocator { public GeoLocation GetPosition () { var locator = new Geolocator(Forms.Context) { DesiredAccuracy = 50 }; if (locator.IsListening != true) { locator.StartListening( minTime: 1000, minDistance: 0); } var pos = await locator.GetPositionAsync( timeout: 20000); MessagingCenter.Send<IGeolocator, AppPosition>( this, "gotLocation", new AppPosition( pos.Latitude, pos.Longitude) });
Table of Contents Address book Reference Xamarin.Mobile * Table of Contents Reference Xamarin.Mobile Accessing Device APIs Geo location Address book Camera (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
Address Book Shared Code Add interface IContactsGetter Use DependencyServices to call methods Use MessageingCenter to Subscribe to event Public interface IContactsGetter { void GetPhonebook(); } IContactsGetter contactsGetter = DependencyService .Get<IContactsGetter>(); contactsGetter.GetPhonebook(); MessagingCenter .Subscribe<IContactsGetter, IEnumerable<AppContact>> (this, [matching string], DisplayContacts);
Address Book Implementation Add device implementation and use Dependency public async void GetPhonebook() { var book = new AddressBook(Forms.Context); var hasAccess = await book.RequestPermission(); if (hasAccess) { List<AppContact> contacts = new List<AppContact>(); foreach (var c in book) { contacts.Add(new AppContact() { Name = c.DisplayName, Number = c.Phones.FirstOrDefault().Number }); } MessagingCenter .Send<IContactsGetter,IEnumerable<AppContact>> (this, "gotContacts", contacts); } }
Table of Contents Camera Reference Xamarin.Mobile * Table of Contents Reference Xamarin.Mobile Accessing Device APIs Geo location Address book Camera (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
Camera Shared Code Add interface ITakePhoto Add assembly Dependency Public interface ITakePhoto { void TakePhoto(); } IPhotoTaker picker = DependencyService .Get<IPhotoTaker>(); picker.TakePhoto(); MessagingCenter .Subscribe<IPhotoTaker, string>( this, "photoTaken", PathPhotoTaken);
Camera Android (1) Add device implementation and use Dependency public async void TakePhoto() { var picker = new MediaPicker(Forms.Context); var intent = picker.GetTakePhotoUI(new StoreCameraMediaOptions { DefaultCamera = CameraDevice.Rear, Directory = "DemoFolder", Name = "myPic.jpg" }); StartActivityForResult(intent, 1); }
Camera Android (2) Override OnActivityResult protected async override void OnActivityResult( int requestCode, Result resultCode, Intent data) { if (resultCode == Result.Canceled) return; var mediaFile = await data .GetMediaFileExtraAsync(Forms.Context); MessagingCenter.Send<IPhotoTaker, string>( this, "photoTaken", mediaFile.Path); }
Camera iOS Add interface and use the Xamarin.DependencyServices using Xamarin.Media; // ... var picker = new MediaPicker(); picker .PickPhotoAsync() .ContinueWith (t => { MediaFile file = t.Result; Console.WriteLine (file.Path); }, TaskScheduler.FromCurrentSynchronizationContext());
Device APIs with Xamarin http://academy.telerik.com
Free Trainings @ Telerik Academy C# Programming @ Telerik Academy csharpfundamentals.telerik.com Telerik Software Academy academy.telerik.com Telerik Academy @ Facebook facebook.com/TelerikAcademy Telerik Software Academy Forums forums.academy.telerik.com