Device APIs with Xamarin
Accessing Phonebook, Location, Camera
http://schoolacademy.telerik.com
Telerik School Academyhttp://schoolacademy.telerik.com
Xamarin apps for iOS, Android & WinPhone
2
Table of Contents
1. Reference Xamarin.Mobile
2. Accessing Device APIs Geo location
Address book
Camera
4
Table of Contents
1. Reference Xamarin.Mobile
2. Accessing Device APIs
Geo location Address book
Camera
Geolocation Shared Code
Add interface IGeolocator
Use DependencyServices to call methods
Use MessageingCenter to Subscribe to event
Public interface IGeolocator { void GetPosition();}
5
IGeolocator locator = DependencyService .Get<IGeolocator>(); locator.GetPosition();
MessagingCenter.Subscribe<IGeolocator, AppPosition>(this, [matching string], DisplayPosition);
Geolocation Implementation
Add device implementation and use Dependency
6
[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
1. Reference Xamarin.Mobile
2. Accessing Device APIs Geo location
Address book Camera
Address Book Shared Code
Add interface IContactsGetter
Use DependencyServices to call methods
Use MessageingCenter to Subscribe to event
8
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 Dependencypublic 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);} }
9
10
Table of Contents
1. Reference Xamarin.Mobile
2. Accessing Device APIs Geo location
Address book
Camera
Camera Shared Code Add interface ITakePhoto
Add assembly Dependency
Public interface ITakePhoto { void TakePhoto();}
11
IPhotoTaker picker = DependencyService .Get<IPhotoTaker>();picker.TakePhoto();
MessagingCenter .Subscribe<IPhotoTaker, string>( this, "photoTaken", PathPhotoTaken);
Camera Android (1) Add device implementation and use
Dependency
12
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
13
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());
14
форум програмиране, форум уеб дизайнкурсове и уроци по програмиране, уеб дизайн – безплатно
програмиране за деца – безплатни курсове и уроцибезплатен SEO курс - оптимизация за търсачки
уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop
уроци по програмиране и уеб дизайн за ученициASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC
безплатен курс "Разработка на софтуер в cloud среда"
BG Coder - онлайн състезателна система - online judge
курсове и уроци по програмиране, книги – безплатно от Наков
безплатен курс "Качествен програмен код"
алго академия – състезателно програмиране, състезания
ASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NETкурсове и уроци по програмиране – Телерик академия
курс мобилни приложения с iPhone, Android, WP7, PhoneGap
free C# book, безплатна книга C#, книга Java, книга C#Дончо Минков - сайт за програмиранеНиколай Костов - блог за програмиранеC# курс, програмиране, безплатно
?
? ? ??
?? ?
?
?
?
??
?
?
? ?
Questions?
?
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
Top Related