Presentation is loading. Please wait.

Presentation is loading. Please wait.

Device APIs with Xamarin

Similar presentations


Presentation on theme: "Device APIs with Xamarin"— Presentation transcript:

1 Device APIs with Xamarin
Device APIs with Xamarin Accessing Phonebook, Location, Camera Xamarin apps for iOS, Android & WinPhone Telerik School Academy

2 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 - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

3 Xamarin.Mobile Download Xamarin.Mobile or install through Components for each platform

4 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 - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

5 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);

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

7 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 - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

8 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);

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

10 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 - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

11 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);

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

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

14 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());

15 Device APIs with Xamarin

16 Free Trainings @ Telerik Academy
C# Telerik Academy csharpfundamentals.telerik.com Telerik Software Academy academy.telerik.com Telerik Facebook facebook.com/TelerikAcademy Telerik Software Academy Forums forums.academy.telerik.com


Download ppt "Device APIs with Xamarin"

Similar presentations


Ads by Google