xamarin mobile

22
Mike Bluestein Developer/Writer Xamarin [email protected] Xamarin.Mobile API @mikebluestein

Upload: mike-bluestein

Post on 15-May-2015

12.877 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Xamarin mobile

Mike BluesteinDeveloper/WriterXamarin

[email protected]

Xamarin.Mobile API

@mikebluestein

Page 2: Xamarin mobile

Xamarin.Mobile

• Cross Platform APIXamarin.iOSXamarin.AndroidWindows Store ApplicationsWindows Phone 8Windows Phone 7.1

• Abstracts common device features

Page 3: Xamarin mobile

Now Open Source!

Page 4: Xamarin mobile

Architecture

Contacts

Xamarin.Mobile

Geolocation Camera

Page 5: Xamarin mobile

Contacts

Page 6: Xamarin mobile

Contacts

• AddressBookRequestPermission

• ContactPhoneEmailAddressWebsiteRelationship

Page 7: Xamarin mobile

Contacts

• Maps to native implementation on each platform• AddressBook implements IQueryable• LINQ

Page 8: Xamarin mobile

ABAddressBookRef ab = ABAddressBookCreate(); CFStringRef name = CFSTR ("Smith"); CFArrayRef smiths = ABAddressBookCopyPeopleWithName(ab, name); CFRelease (name); int count = CFArrayGetCount(smiths); for (int i = 0; i < count; ++i) { ABRecordRef person = (ABRecordRef)CFArrayGetValueAtIndex(smiths, (CFIndex)i); if (ABRecordGetRecordType(person) != kABPersonType) continue; NSString *name = (NSString*)ABRecordCopyCompositeName(person); NSLog ("%@\n", name); [name release]; ABMultiValueRef phoneNumberProp = ABRecordCopyValue(person, kABPersonPhoneProperty); NSArray* numbers = (NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProp); CFRelease(phoneNumberProp); for (NSString *pvalue in numbers)

NSLog ("Phone: %@\n", pvalue);

[numbers release]; ABMultiValueRef emailProp = ABRecordCopyValue(person, kABPersonEmailProperty); NSArray* emails = (NSArray*)ABMultiValueCopyArrayOfAllValues(emailProp); CFRelease(emailProp); for (NSString *evalue in emails) NSLog ("Email: %@\n"); [emails release]; } CFRelease (ab); CFRelease (smiths);

Contacts - iOS

Page 9: Xamarin mobile

Contacts - AndroidContentResolver content= getContentResolver();

Cursor ncursor = null;try { ncursor = content.query (ContactsContract.Data.CONTENT_URI, new String[] { ContactsContract.Data.MIMETYPE, ContactsContract.Contacts.LOOKUP_KEY, ContactsContract.Contacts.DISPLAY_NAME }, ContactsContract.Data.MIMETYPE + "=? AND " + ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME + "=?", new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, "Smith" }, null);

while (ncursor.moveToNext()) { print (ncursor.getString(ncursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)) + lineSep); String lookupKey = ncursor.getString (ncursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); Cursor dcursor = null; try { dcursor = content.query (ContactsContract.Data.CONTENT_URI, new String[] { ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.NUMBER,

ContactsContract.Data.DATA1 }, ContactsContract.Contacts.LOOKUP_KEY + "=?", new String[] { lookupKey }, null); while (dcursor.moveToNext()) { String type = dcursor.getString (ncursor.getColumnIndex(ContactsContract.Data.MIMETYPE));

if (type.equals (ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) print ("Phone: " + dcursor.getString(dcursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)) +

lineSep); else if (type.equals (ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)) print ("Email: " + dcursor.getString(dcursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA1)) +

lineSep); } } finally { if (dcursor != null) dcursor.close(); } }} finally { if (ncursor != null) ncursor.close();}

Page 10: Xamarin mobile

Xamarin.Mobile Contacts

var book = new AddressBook ();

foreach (Contact c in book.Where (c => c.LastName == "Smith")) { Console.WriteLine (c.DisplayName);

foreach (Phone p in c.Phones) Console.WriteLine ("Phone: " + p.Number);

foreach (Email e in c.Emails) Console.WriteLine ("Email: " + e.Address);}

Page 11: Xamarin mobile

Geolocation

Page 12: Xamarin mobile

Geolocation

• Geolocator• Position

LatitudeLongitudeAccuracyAltitudeAltitude AccuracyHeading

Page 13: Xamarin mobile

Geolocation

• Geolocator class• Retrieve current location• Listen for Location changes• DesiredAccuracy influences the location technology that is used

Page 14: Xamarin mobile

MediaPicker

Page 15: Xamarin mobile

MediaPicker

• Take Photos and Videos• Select Photos and Videos• Programmatic feature detection

MediaPicker.PhotosSupportedMediaPicker.VideosSupportedMediaPicker. IsCameraAvailable

Page 16: Xamarin mobile

MediaPicker Camera

• Specify which camera to use• Specify video quality• Async and C# TPL Compatible

Task.ContinueWith, IsCancelled, IsFaulted

Page 17: Xamarin mobile

MediaPicker iOS

var picker = new MediaPicker();

MediaPickerController controller = picker.GetTakePhotoUI (new StoreCameraMediaOptions { Name = "test.jpg", Directory = "MediaPickerSample"});

PresentViewController (controller, true, null);

controller.GetResultAsync().ContinueWith (t => { // Dismiss the UI yourself controller.DismissViewController (true, () => { MediaFile file = t.Result; });}, TaskScheduler.FromCurrentSynchronizationContext());

Page 18: Xamarin mobile

MediaPicker Android

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

Page 19: Xamarin mobile

MediaPicker Android

protected override void OnActivityResult (int requestCode, Result resultCode,Intent data)

{ // User canceled if (resultCode == Result.Canceled) return;

data.GetMediaFileExtraAsync (this).ContinueWith (t => { if (requestCode == 1) { // Video request ShowVideo (t.Result.Path); } else if (requestCode == 2) { // Image request ShowImage (t.Result.Path); } }, TaskScheduler.FromCurrentSynchronizationContext());}

Page 20: Xamarin mobile

var mediaPickerController = mediaPicker.GetPickPhotoUI();

mediaPickerController.GetResultAsync() .ContinueWith (t => {

mediaPickerController.DismissViewController ( true, () => {

// User canceled or something went wrong if (t.IsCanceled || t.IsFaulted) return;

// We get back a MediaFile MediaFile media = t.Result; });}, TaskScheduler.FromCurrentSynchronizationContext());

MediaPicker Selecting Photos

Page 21: Xamarin mobile

Demo

Page 22: Xamarin mobile

Resources

xamarin.com/mobileapi