Tip:
Highlight text to annotate it
X
Hello, everyone, and welcome to
the Xamarin seminar today. Uh, we're going to be talking about Xamarin.Mobile, which
is a way of accessing a unified cross-platform features
uh, within your mobile applications.
It's the fourteenth of June today.
Um, it's a pretty gray day outside in Manchester.
Um, we have Mike Bluestein who's going to be, uh, doing the seminar for us today.
He's out in San Francisco at the moment. Unfortunately, not at WWDC.
But he is there. I'm sure he's probably been
outside to try and peep in and see what's going on there. Uh, he's, uh, if
you haven't been to a seminar before
he's a technical writer
as part the documentation team at Xamarin. You can email him at
mike.bluestein@xamarin.com
And you can follow him on twitter @mikebluestein.
Uh, so I'm gonna
introduce and, uh, let Mike, uh,
introduce the subject for us.
Thanks, Chris.
So good morning, good afternoon, uh, everybody.
So today we're gonna be talking about
Xamarin.Mobile, Xamarin.Mobile API.
And what that is it's an API that allows you to
access certain
device and, uh,
some stock UI features across various mobile
devices without having to change your code with one - one codebase or with
very minimal code change in some cases. And we'll see some examples
that, uh,
um, that will demonstrate what – what you can do with Xamarin.Mobile.
So
I'll just mention that this API is currently in pre-release form
so there's a beta version of it up on our site and I'll provide some
links at the end of the
session. So when we say cross-platform and cross –platform mobile what do we mean?
Well, the cross platform A, uh, Xamarin.Mobile API works on Mono, um, iOS, via our MonoTouch
product
and likewise we have, uh, an implementation of the same API
for Android,
using Mono for Android, as well as Windows Phone 7.
So the idea here is that you could use Xamarin.Mobile
and it - it
over about just being able to use C#, uh,
dot, uh, Mono in dot net framework base class library, uh, capabilities to
take
non UI code
across these different platforms.
This takes it one step further and provides access to a variety of other
features,
uh, some -
some involving, um, certain parts of the UI
uh, that are stock or provided by the system
in a - in a unified, uh, API, an abstraction
so you'd only have one code base.
And that only not only gives you even more of a code re-use story,
but the API is actually shaped to simplify
um, the various device and platform specific API's
as we'll see when we go through this session here.
So what - what does it offer?
So the – it's available in Xamarin, uh, Mobile, Xamarin.Mobile is – the
the name of it, that's the assembly
and there's various namespaces. It's under the Xamarin namespace.
If you download it you'll see.
Uh, there's – there's contact support so all the
you know, these - these different mobile devices all have
some - the concept of a contact or an address book and
they implement them very differently so that's
that's enabled through
uh, the - currently in the pre-release version
that you can go download today and we'll see an example of that.
There's also geolocation.
So that's - that's basically your -
your different location technologies like GPS
or, you know, cell – cellular triangulation or Wi-Fi triangulation. That's all
abstracted and available on – on
on iOS, Android and Windows Phone.
And with, uh, the
Z – the Xamarin.Mobile geolocation support you get one API to access those
sensors.
So it's kind of nice.
We're planning on introducing other additional sensor support, things like
the Compass and the Accelerometer.
Um, you'll see it slightly – it's greyed out here in the slide. That's not
currently available
in the - the bits you can download now, but it will be coming in the future
release
as well as camera support.
So now here's - here's something that does involve a user interface, of course.
Right? I mean but it's a stock user interface in that there's the camera so it's again it's a
sensor
in a way. Right? It's a device specific hardware capability that's abstracted
through some API
on each platform
and what – what Xamarin.Mobile does is it – it provides access to the – the
obviously the native camera, but also the stock, uh, user-interface that
you'd get to pick
Media, pick photos, take video, things like that,
um, on that particular devices through, uh, a single unified
uh, code base, an API. So that's available as well
in the pre-release and we'll see a
example of, at least picking photos because of I'll be running it from a
simulator where the camera's not available.
But on a device you'll have that
full capability.
And the last thing that, uh, currently is planned to, um, add into this will be
notifications. Again, it's - it's not - that's not available in the
uh, the current bits, but it'll be coming in the future.
As well as if - if people have, um, ideas or - or they'd like to see
additional things or they'd like to influence how this is shaped please,
um, your feedback is, uh, is more than appreciated so.
So let's start with, uh,
talking about contacts.
So what the
Xamarin.Mobile Contacts does is it provides provides a
a layer on top of, you know, contacts, the address book basically. Right?
And what it does it maps to it the native implementation on each platform.
So underneath the covers, you know, you have a
there's – there's – there's AddressBook framework on iOS, there's – there's Windows Phone
Implementation,
there's – Android has their
their way of going at things on things through, you know, content providers. How – how you do it on the
native platforms. Right?
And each of these things
uh,
uh, provide the same kind of
information conceptually,
but they're implemented very differently.
In some cases they're additionally implemented in – in
API's that are a little bit verbose. We'll see an example of that, uh, specifically
the AddressBook API in – in iOS is very powerful, but you have to write a bunch of code to do
what could be a, uh, uh, a little bit of a simpler thing.
Um, so
what we do is this abstracts all that in that it
not only gives you that unified codebase, but it additionally gives you a,
uh, a little bit more of a concise API.
And you can get that through – through tech, you know, the technologies that, you know, that we
sit on in C#. The C# language gives you the capability to do things like
LINQ, right. So the AddressBook class which is part of Xamarin.Mobile.
Um, implements like IQueryable and that's
so you're going to be able to use LINQ to query on top of AddressBook. So that's a
big
uh, benefit to making the code really readable and concise
uh, an maintainable code.
So let's take a look at some examples or some code side by side.
So not – not important to go line-but line through it here, but this is just a – a code
a code snippet that shows you how to use the
native API on Android
to basically, uh, query
the other contact list in – in your Address Book on,
uh, Android and you're going to find – and give me all - all my contacts that have this
last name, Smith.
Alright? And there's some Java code that does - that goes through that.
So that's what that would
look like.
And here on iOS, here's using the AddressBook framework
using Objective C and
uh, there's, you know, Objective C sits on top of C. It really is C. It's a
super set of it. So
there's a C API and there's the foundation classes and there's some Objective C around
that.
And that's the code that you use to enter, uh, to get at the AddressBook and
give me all the, uh, you can see up on the top, the second line there, its – it's
saying it's
Smith and let's give me all the people in my AddressBook that
have Smith and let's enumerate them, do something with it.
So if you, uh, were to use the Xamarin.Mobile
Contacts API here's what that
same code would look like.
So you can use this AddressBook class,
right, and then
I'm not doing, you know, anything user – UI specific here, I'm just going Console.WriteLine()
but then I think you can use, um, a LINQ
and you could just query,
you know, using the AddressBook to give me all the contacts with the last name Smith.
And then I can – I get a contact back and I can do things, you know, take the data from that
contact to do what I want.
You know, their phone numbers are available,
addresses would be available, emails, things like that.
Even as well as you could get like thumbnail images or preview if you have
those in a – their, uh, the particular contact,
um, list. So
you can see that, um, if you – if you,
you know, if you saw the first two slides the code was quite,
um, quite a bit and
you can see how- how, you know, in this particular case anyway, it really - it really shines to
show how -
how you can bring in the code down to a nice concise, uh,
um, readable form
and taking advantage of the nice features, nice language features
like on C#.
So
let's go on to the next part and let's look at a -
let's look at an example here.
So this is simple example and it's, uh,
uh, all this – all the examples here, uh, be available – are available
for download with, uh,
uh,
the pre-release version of Xamarin.Mobile that I'll put up a link to
at the end of the talk.
And the -
the – on the left here you see a screen shot of, uh,
some code running in the iPhone
and it's just a simple application that just
Enumerates and puts the – the – your contacts in a list and then
goes to a detail screen.
And you'll see on the right,
uh, same – same kind of application using,
uh, Xamarin.Mobile to get your contact list in
Android.
And same idea putting the – the - there's a detail screen. If you tap on one of
The -
one of, um, contact's names
you'll get navigated over to a detail screen. I'm not showing the screen shot,
but
there's a – the implementation in the sample for Windows Phone 7
is available in the download as well.
So let's take a look at that example and we'll just
go through a couple parts of the code to show you how that would work.
So let's jump
over to MonoDevelop. I already have it open. So
here me, uh, let's just run it first before we -
before we jump into the code. And we won't go through the whole example because you can, you know, there's –
there's a variety of cases, but I'll just highlight a couple of the important parts. So here
for - for brevity I'm just gonna run everything in the, um, iOS, but
again, it's available,
uh, the full example's available for Android in the download, as well as Windows
Phone 7.
So
here's, um, the user interface we just saw
and I – I'm in my simulator so I just made a couple contacts in there
and if you tap, you know, simple, you know, native UI still right, so I'm -
I'm using a UITableView, UITableViewController,
um, in this particular case, right. In Android I'd use a list view and –
and so on. You could do it with say a stack panel, things like that and
if you were going to go to Windows Phone 7.
So in this case though the way I get contacts though
is through instead of going through AddressBook I'm going through, uh,
Xamarin.Mobile.
And then I go to detail screen and I can get some more information about the
particular contact
here. You know, simple application. So
let's, uh, close that. Let's take a look at
the key things you – without going through all the code.
Uh, let's go to the
couple key things you need to know about. So there's this AddressBook class
so if you're using, um, you know, the – the name space up in here is, uh,
Xamarin.Contacts up on top.
And then I have - the UI I'm still doing in this case, you know, real simple just for
the example, but I'm doing a native UI so I have the – on iPhone I have a UITableViewController
again and the UITableView.
And then I, um, you know, nothing universal about that. You develop the UI specific
to the particular platform so it's native, not out of, place, good - higher performance.
The same story we've talked about, um,
uh, in – in past seminars and if you go through our documentation you,
um, can read more about that.
But now the way I go at contacts here
is I actually go through this class called AddressBook.
Right? And by using AddressBook now now I don't have to go through, um, the - the
native -
the native API for AddressBook, um, on the particular platform. I can just use
that
and I can just say hey in this case
I just said give me all the, uh, contacts where,
uh, the -
the contact has of a mobile phone.
Right? And give me the first ten of those.
Uh, just a – sort of a contrived case just because I put a couple users in my -
my, uh, simulators Address Book here.
But it's like you can see though, you can do - can do all the kind of
link, powerful LINQ things that you'd -
you'd want with, uh,
the – the AddressBook API and you get back a contact
and a contact is the model in Xamarin. Mobile that has all the, you know, logically
models your contacts. So it has their first name, last name,
all their phone numbers, uh, what type of phone numbers they are, their address.
And you could even
if went to the detail screen here, I'll – I'll jump over to it here,
you could use the contact when they, you know, um, too as well. There's a method,
uh, is it down here?
That, yeah, here's the contact. I think it's passed into this detail screen.
And you can go get thumbnail if the contact had a thumbnail to present it. In this case I didn't have
one in the simulator, but.
So all the type of things you'd want when working with contacts.
And that code is, you know, so this code here, you know, to interact with the contacts and created in
AddressBook,
that's your universal code.
And then the rest of this example is just
how I'm going to then do it in this particular user interface and that's
specific to the platform.
In this case I'm using a UITableViewController.
So no – nothing, um, nothing about Xamarin. Mobile there.
But then I'm working on Contact.
So, uh, you work with
Contacts and you can see
that code, that even that – for that simple case would be,
as you even saw in the screenshot, that's quite a bit a code
if you're using AddressBook framework, um, at least in IOS and it's quite a bit. It's even -
depending on what you're doing could be even more code on Android. So, um, you can see
other than - other than the universal, you know, reuse nature of it it really,
uh, shapes the API down to where it's – it's quite simplified
and approachable, but yet
still powerful via the,
you know, support for, uh, iQueryable and LINQ.
So
with that let's jump back over to
our slide still.
Let's continue on. So that's a simple example using, uh,
how do I access AddressBook information through, um, Contacts and Xamarin.Mobile.
Next thing I want to talk about is the
media picker. So,
un, on – on all these – on the few platforms we're discussing,
you know, they all have a – they all typically have cameras, right,
and, um, you can take a vi – you typically take video with them,
you can take, uh, a picture with them and then you can likewise, select video or,
um, photos that are stored on the device
and you get some stock user interface to select that media.
Right?
So you say, hey, I want to pick a photo,
you'd get some user interface that's presented from the particular platform.
So it's a – it's a U – a UI, it's specific to the particular platform,
but it's a stock UI
delivered by the system,
right, by the particular platform.
And then you –you'd get an image back, like raw video, your media, right?
And then, you know, you can do what you want with it at that point. You can put it in the user interface at, you know,
you're in your custom,
uh, application code.
So what the MediaPicker does is like, um, it abstracts all that.
It abstracts taking photos and videos, right, and dealing with the, um,
the -
the stock user interface. So if you – on – on the iOS you'll get
the proper - the proper controller there that -
that - underneath the covers that'll allow you to select media.
Same story on – on Android and so on. So it allows you to take -
take photos and videos
as well as select photos and videos. And we'll see the selecting a photo, uh, in our example in
a moment.
So it also has programmatic feature detection.
So, um, um, what that means is
you know, if you move to say an
uh, certain, you know, especially like say Android, some devices have -
have video. Most of them would have – have the full support for video
and, um, cameras, but if you get in an older device or if you're on a tablet or
some - some device that doesn't have that capability,
perhaps a simulator.
Right? So if I go in the simulator I'm not going to be able to take video in iOS.
So we have support for that through a couple things here. There's MediaPicker Photo
Supported
and there's likewise for video, there's, uh, a Video Supported
property you can get.
So, you know, nice simple access, universal
to – to determine across the board on these devices
and platforms, um, what capabilities are supported so you can handle things
properly in your code.
So
with that, let's go over to a, um, example now
um, of taking photos. So actually - actually before I take a step back
I just want to show you what the, um, code looks like for selecting photos.
So there's a MediaPicker class and we're going to see that in the example. And then
once I create a MediaPicker I can just call say pick photo,
um, async and then there's this continue with – and that's just –that's where I'm going to get my callback there.
I can get back, uh, um, this result which is going to be a media file
and I could use that to then populate and, um, get a bitmap that I can use in
my UI to do a UI specific thing like, uh,
you know, whatever – I call it platform is their image there. So it's whatever kind of
way you, um, you represent an image on the platform. You can then
you know, present it and then show it in the user interface. And we'll see that in an example.
And you can see here on the right
there's the actual, uh,
um, native stock user interface that's abstracted away.
So you can see up in the – the first screen shot there's Windows Phone
and then next to that we have the, um, the iPhone
with the, um,
media picker there and then down below there's a, um, Android, uh, view from
from an Android tablet.
So you can see – you can see the user interfaces different that's coming,
um, from the particular systems, but abstracted by our
MediaPicker API when you call on pick Photo. And likewise there's a - there's a
there's an analogist call for picking media
and there's also, um, so more calls for actually taking
photos and taking media as well. But the important, uh, takeaway from one is there is use -
some bit of user interface, but
it only extracts, um, user interface when it's stock
or system provided. That's what I simply meaning by stock
user interface so,
if you're going to develop, uh, your custom user interface, that's where you
develop.
Now you're - now – now that's where you'd step over and above this API
and you'd developed it native for the platform. For example, like we had in the previous
example with the UITableViewController.
So now let's, um,
take a look at, um, um, um, taking
photo - photos or videos. So and let's talk about it a bit.
So you can specify which camera to use, so you can -
whether you want to just say use the, um, um,
you know, front or back and you can query for camera availability, specify video quality. Right?
So if you want to just say like have a scenario where you're going to be taking
a
um, a video and you were gonna – you didn't want to take a lot of space or
You might want to upload it you might want to upload a snippet of it, that's useful.
Likewise, with, um,
there's full support for
asynchronous support and support for TPL, where it says TPL compatible. So you
can do things like, you know, there's tasks
and so you can do tasks, uh, it's cancelled, it's fossiled -
It's faltered, excuse me.
Continue with.
And likewise, there's synchronization support that
comes with tasks. So we'll see that in one of our examples later
um, so to give it a, um, capability that will allow you to, um,
um, sync automatically to the caller's
thread – to the – the – the thread of the caller's contacts.
So
that's what I talked about there. Here's an example
of, um, taking
a photo or a video. In this case I'm querying in the first line for
if the camera's available. Right?
And then I'm going to create this thing called a a VideoView since this is an Android example
in the second line.
And so that's – that's where I'm going to have my native UI that I'm just going to
create myself for how I'm going to actually present
particular video.
And then I can take video and then again we'll see an example taking a
picture we'll see it in iOS.
That picker
take video async, that's the thing that's going to launch
the user interface for actually doing video.
And that code in the middle there with the picker take video async and then continue
with
that's all reusable and that's all the universal part of it.
And in the part that I deal with VideoView there, that's where
I'm actually now going to put the video into my native UI wherever I want it.
Alright, so the – the code that's going to interact with,
um, device capabilities, either device stock UI or native sensors, things like
that
that's the kind of code
that Xamarin.Mobile extracts
and is universal and re-usable across all these platforms and in the code where you actually take the
Results and put them into your custom user interface,
um, such as the VideoView on Android and this particular, uh, code
snippet, that's, again, the custom code that you'll write
specific for each platform so.
Now
well that say, so there's -
there's – there's – there's – there's video taking, there's
photo taking,
there's video picking and photo picking support in there.
Here's an example we're gonna jump into
we're not going to, um,
do the taking of video or taking a photo because I'm going to show it in the iPhone
with that capability's not supported, but it's handled
via the availability, uh, you know, the – the availability checks that I showed
when you can see that in the example.
And we'll – we'll do example there where we'll pick a photo,
it'll launch the – the stock user interface to pick a photo on the
particular, uh, platform
and then getting that photo back. We'll put it in in this case we're just going to put it in UIImageView.
And we see we again we have the same application
built on Android.
Um, there's one I had just done
and to took a photo of outside the Moscone Center and
Apple has the, uh, the - the conference that they're running right now. I have a neat picture there.
And again, you get the -
the second screen there is the detail screen. When you do pick photo you'll get the -
the stock user interface and that will allow you to select media, a photo in this, um, case as well
for selecting.
So let's take at, uh, jump back to the code and take a look at that, uh,
key parts of that example.
Let's just run it here.
So if I go pick through
here's the – so I'm on the iPhone so I'm going to get the media picker,
uh, view controller. And then I see I have some saved photos that I put in my simulator
I'll take it and it comes back and then there's my custom view and have a UI
image view and I have a UI image view and
I can get the UI image back. Oh, um, um, the – the return
It's actually a thing called a media file that comes back from
the, uh, Xamarin.Mobile and I can then out it into my custom user interface which is just
a, you know,
a simple thing he with a UI image view.
So it's time – close that and we'll take a look at
How we do that. I'm sorry, I'm in the wrong tab.
So, um,
here's, uh, that particular example. So
with Xamarin.Mobile, um, the – again, just like you had AddressBook there's a top level class
for contacts.
The top level has to deal with media
is called a MediaPicker. Alright? So I create a MediaPicker and that's the thing
where every - everything can be done, where you can,
um, select your
at least for selection. So I can - I can
pick my photos so.
I'm gonna do a check here to see is our photo Supported. If it's not I'm going to some -
write – run some code that's going to handle it. This can just put – put up an alert view. We don't need
to show that.
And then I have a call down here
to, uh, pick the photo asynchronously.
And this is interesting. So you can see
again we, uh, we pickers, so this is my MediaPicker,
and it'll pick a photo asynchronously. And then I'm using, uh, I'm using TPL, so I've got a task here and the
Result, yes I'm going to go continue with, and then what - from then I'm gonna
in the task I'm going to get back this result which is just a media file.
Um, that's the – if we can mouse over here, right here. Yeah. So Xamarin.Media MediaFile is
the result.
That has a path
I can then use to write code
on the platform I'm on, which is iOS in this case
to get UIImage. Right? And I would do that and so I'd get the result back which is the
media file
and then use that
for the platform specific code to get back the – the platform specifics image
representation which I want to create a UIImage on iOS
and I'll put that in – into a user-interface, which
on iOS I have an image view that was added and I'll set its image property and
that's how we – when we selected the image it gets put on the screen.
So that part, you know, you're dealing with UI image and dealing with the custom user interface
again
Highly, um, native to the particular
applications
uh, platform the application's running on, but the code to actually
um, present the MediaPicker
and select media and get the result back is universal and can – and present the
proper thing on each platform. Likewise, we have in the example there's the same thing
for video,
um, selecting it – it's basically the same code for the most part just
you just, um, different method. You're picking video
as well as taking – there's a, uh, now this calls for to taking photos and taking video. Quite
simple and – and actually
again this, um,
this goes a little – it's not quite as verbose as what – what we have the example where we
compared
the – the contacts in the AddressBook, but again, it is a – a – a
reasonable reduction
in the amount of code. So in addition to the -
the, uh, the reusability support of the whole thing again
you're getting a – a simplified API to do the exact same
thing. So it's cross platform in a –
a more concise, uh, API that's shaped
for you with Xamarin.Mobile. So
that's some
using, um, a simple example using a photo and using media
with – with Xamarin.Mobile. Excuse me. Let's, uh,
go to the third thing that we're gonna talk about here today
which is, let's go back to the slides,
geolocation.
So talk a little bit about geolocation, um, before we jump into Xamarin.Mobile. So
geolocation mean – means locating, uh, the position of the device
and on Android, um, well it varies based on the type of device that we have to have, whether you
you have
whether you have a phone,
there's three basic technologies that
are already abstracted
um, in the vendor specific ways by Apple or Google, you know, Microsoft on – on
iPhone
iOS, uh, Android and Windows Phone.
And those three technologies are
GPS, GPS sensor
Uh, Wi-Fi triangulation or the – or the cellular radio.
So there's three different ways that
you know, um, typical ways you can get uh, ways that you can get
uh, location.
And they already have – each platform has API's that already sit on top of these
three different technologies so
cellular is the
most coarse. It's going to be the least accurate.
It's going to do the best on battery life. Right?
Um, Wi-Fi sits in the middle so it's a little more accurate than
cellular positioning, but
it's going to use a little more battery, um,
and then GPS, um, typically works outside is the most very accurate, highly
accurate,
but consumes the most battery life. So,
you know, the – it depends on the case that you're going to be using it
for. If you're making say a turn-by-turn
application where you're going to be plugged in and you're in your car driving around, you know,
navigation out,
um, that – that's suitable for GPS. If you just want to get
you know, get some location on an city or what -
excuse me, somewhere in the neighborhood of, um, some particular point of interest
there. That – that could be, you know, either Wi-Fi or cellular, depending on
um, what – how accurate you need things. So
there's already
platform support
for an abstraction on top of location
and the different technologies on – on all – all of these platforms. Right? And
then also support for what's available like, you know, some versions of iPads and Tablets,
things that don't have a GPS so,
you know, and other -
and few other devices, too, iPhone say has everything.
Yes, same – same thing. Windows Phone or certain Android phones. So
how that – that's abstracted, but note that again that it's very different though. You move from –
even though it's – it's basically the same physical technologies
that they're using. These things have to work the same way.
There's very different API's, of course, because different companies they shape them, not just different
languages, but very different API's.
So again, that's where Xamarin.Mobile will come into Play.
It - it again, in this case there's no UI. It's just totally abstracted in the
sensors, but it's doing it by sitting on top of the native implementation on
each particular platform. And the class it uses is called a geolocator.
So just like we had AddressBook and we had the, you know, the – the Media –
the MediaPicker, MediaFile
here we have a class called a Geolocator.
And also, again,
the same story, it – it not only gives you the – the code reuse and the
universal,
um, support,
it again is a simplified API. So it shapes,
um, the API so that code becomes, uh, a lot more concise.
And it allows you to retrieve
current locations so you can,
you know, enter, um,
directly, you know. Say, hey, I want the location right now of where I am. You can also have, uh,
has a listener so there's a couple call backs
where you can say hey give me, uh, a location
when things change and, you know, the tolerance of when things change is,
uh, is, um,
uh, configurable
right, through the API. You can say what kind of
based on um, um,
uh, used, based on input that you put in your code, right, and this desired accuracy, things like that.
All the kind of things that are supported in the native API so,
but abstracted up through this nice, uh, reusable library.
Right. So you can listen for location changes.
Um, and
so – so again I – I already mentioned that it – there's a desired accuracy
property and that
what – what the desired accuracy is going to do,
um, is –
here. That's gonna influence the location technology that's used.
So picked – a minute, uh, a couple moments ago I talked about the different
technologies and
how, um, they vary, um, going from say cellular to Wi-Fi to GPS. They
have – you – you're going – you're increasing inaccuracy,
um, in – inversely proportional to that. As you increase inaccuracy
you decrease battery life so they'll consume more power.
So, um, you have to be cognizant of that as you're developing your applications, but
the way you can influence, you can't
always specifically –
you can't guarantee that you're going to get just this particular thing in all
cases,
but you – you can actually, but the way you would, um, say hey, I want GPS or I'd like GPS,
give me the best
location available and my application can deal with battery life because that's the
case that I'm
building for desired accuracy
going through the geolocation API's and Xamarin.Mobile will allow you to influence,
um, what technology is picked internally
by the native implementations.
So, again we have an example here
that's a simple example. So just a simple UI that I'll show you.
Um, first thing you can do – it'll do is you – you can start the listeners so there's
some callback that'll show, uh, the location as the location changes, right,
and then if the – up above you can actually say, hey,
give me, um, the particular location right now and it'll just go off and it'll just
asynchronously -
asynchronously call and get the location. And again, there you have a, you know, very simply
UI put. For this case it's throwing in some labels.
But we have it for
iOS and have it for Android and – and as well as in the sample that you can down – that, uh, will be
available to download.
Again, same story, you have it for Windows as well.
So
with that let's jump
back over
to the code here
and I'll run that one again and they'll again show it to you.
So I'm in the, you know, simulator here so the -
the it's,
no pun intended, the – the location is simulated. Right?
So all I do is I start listening and,
you know, just mock give you a mock location. I think it's somewhere in San Francisco
or it's in Cupertino, I haven't checked, but
you know that the iOS simulator gives you for a -
a location,
um, but just mentioned that – that if - if you go look on the site there's also tooling, um, there's – there's
support on all these platforms for,
um, being able to mock various different locations so you can develop a,
um, location-based applications a lot easier these days, um,
without having to have a device and say run around different
places, although you'd eventually need to do that.
Uh, and here up here I can go get position and I can Just, you know, directly make a call
and say give me the – give me the location and I can cancel the listener and that would stop itself.
Pretty simply application to demonstrate it.
So let's, um,
come over to the code here, let's make this bigger.
And I'll show you what the classes are that are
the key ones to work with, uh, uh, Geolocation in Xamarin.Mobile.
And the top level class you'll deal with is called the Geolocator. Right?
So here I just created a –
I'll come back to, uh, this task schedule of business in a second.
It's done a little different there.
And here just to show you some of the additional TPL support
other than just doing continue with and tasks,
um, that's,
uh, kind of cool.
Um, you have the Geolocator class and then there's that DesiredAccuracy
that I'm setting and that's going to – that's what's going to be influence,
um, um,
what – when – when you're going to get, you know, what - what kind of a – what kind of, um,
radio's going to be used underneath, whether it be GPS or
you know, the – the more accurate you're going to, um, set that,
um, the
harder it's gonna, you know, quote unquote, say try to
use that particular, uh, sensor.
And then I have a couple of events here on the geolocator that when I start listening I'll get
call backs.
I can get when – if there's an error, uh, a listening error so I can handle that.
Um, and there's a, uh, a custom event argument that you'll get back in the call,
what you'll see down below,
And likewise, the position change. So if they're moving around
it changes
um, distance and you'll get it.
And other – I'll just come down. There's this simple hander - handler
where I just, uh, and in those two call backs I just go,
um, asynchronously to – to begin to – to put it back to the UI thread
and I'm just going to update some –
some UI, which is just a couple UI label views,
uh, UI labels that I had,
excuse me, in this case.
The, um, the – the event args right here comes from – from the API, from the Xamarin.Mobile
API and that's where you can you can get position back. So you'll get back this class in the
event args and that has information about the latitude and longitude of where you are
or an error
if you came through the, uh, if there was some kind of a problem.
Now likewise to start that so – so to get those events you'd call the geolocator start
listening
and that's where you can set. So I - I've just got to
confuse the point that the desired accuracy is what influences the radio.
Um, the call you make in to start listening is where you can set tolerances of when you get,
um, the delta of when you'd actually get
the changes that, uh, to come back because you can imagine if you were -
if you were – if I was saying give me a location change and I was doing GPS I had a real
high accuracy
and I want a location change every foot.
So you – you could get a lot of calls back so
having a little bit of tolerance in between allows you to both
reduce the number of call backs and that
even – even for the cases where you're
working with say GPS, um or, you know, real high accurate,
but highly power, um,
power consuming, um,
technology. You can – you can try to design for that a little bit by
just having a little bit of tolerance put in
so it's, you know, not more than what's practical
to the end user
um, the amount of data coming back and that can help your battery as well. So that's all -
very simple.
And again, if you did this code in the -
right in the native – native implementation, um, at least in iOS here,
um, that it's sitting on top of
it's a - it's a – it's not hard, but it's – it's, uh,
it's quite a bit more code.
So, um, again, same story, I keep - keep saying it, but,
you know, reusing code, reusing code that make sense to reuse
across these platforms, uh, already kind of built and
tested for you, but
also simplifying the API down.
And then so that was that the listening part of that. And the last part I want to show
is actually press the button and say hey give me the location right now. Right now meaning make the call
to give me it right now and it does it asynchronously. That's done with this - that
GetPositionAsync.
Um, and same – same business and arguments you can
put in there and you can time it out.
So if you like, you did it asynchronously and didn't – you didn't get call back, uh,
uh, in a certain amount of time you won't just sit there forever.
Then you can handle some errors in there and same – same business where you
have a - a result which is just a position.
So just like the, uh, uh, in the event args that to come back to the listener
handlers. Um, the one thing I'm differently here, just to mention
to – to drive home that
um, I'm using continue with and I'm passing in task.
I'm also passing in the scheduler caller. So you see where – how I'm,
you know, um, doing InvokeOnMainThread
to,
uh, synchronize to the caller, to the UI thread in this case in this case. In this case I'm
actually just creating a task scheduler and so I can, you know, [INAUDIBLE] that – that'll,
uh,
that'll automatically put you, uh, put this code in the middle
back into the context of the callers thread so.
That's supported as well and available again on - on all the platforms.
So that's the last example I wanted to show.
Let's jump back over here.
And I wanna, um,
come up here and just show you the resources. So if you go to Xamarin.com/mobileapi
there's, uh, a discussion there about, um,
you know, the stuff we talked about here today and
uh, basically posted explaining what, uh, Xamarin.Mobile is
as well as these links that are on that page. And I just put them up here for
convenience. There's a -
a download link there which you can download the -
and this, again, is a preview so it's –
it's not to, uh, release state yet, but you can totally use it, um,
um, in commercial apps, fully available.
Uh, there'll be, you know, obviously changes coming to it, um,
most - mostly
you know, there'll be that new support, uh, for different capabilities such as
uh, notifications, things like that.
And, um, you know, any – any – any additional things based on feedback we get from you.
So that – that download link is on that Xamarin.com/mobileapi page
as well as there's some API docs as well
that we have. So you can look through, uh, all the classes we saw today and you can see the
methods of the
different properties, things that are available,
typical API docs. So again those are linked to from there. And if you take – if you get that down and
you'll see that all the examples we went through here today
are available in
that download and as well as they're, uh, there's a couple other things we didn't cover
like, you know, we talked about it, but we didn't cover the video , taking video just because I'm working in
a simulator
so that'll – you'll be able to do that. If you have a device you can put it on and try that out
as well as the, uh, uh, full Windows Phone 7 versions of -
of all three of those examples are there as well.
So
thank you everybody for, um, um,
joining us this morning, this afternoon, based on where you are.
And, um, I'll just had it back over to Chris now. If, uh,
anyone has any questions.
Thanks a lot, Mike.
Uh, we have, uh, two questions.
Uh, the first question is does the geolocator support reverse geocoding?
Yeah, I'm not sure. I'll have – I'd have to check in the API. I – I
don't think it – I don't think it has, um,
I don't think it currently has support by that, but we'd have to –
Or we could look in the API.
I'm not sure. I'll post the answer to it. Um, I have – I don't think it has that
support yet.
Yeah, I had a quick look myself and it doesn't seem like it does support reverse
geocoding,
um, but that's something… But I do think that's on the list to add. Yeah.
Um, and the other question is can you get access to the raw camera data
so you could use a
QR code or barcode library for example?
Sure. So that's – yeah, you can – you can get – you basically get a, um, um,
you know, Bitmaps of everything coming out and you can –
that - that's what I even did it in that example.
So, you know, once you have the image because again that – that –
the – the API that's getting you the data back from the camera is that.
And then at that point you do what you want with it. So in this – in the
simple example all I did is I then –
I then turned it into a – a UIImage on iOS and I put it
in a UIImageView, but you could do whatever you want
at that point.
Yeah, I guess you can't get the –
the live camera data
um, feed, um.
Oh, oh, I – I – is it – I might have misunderstood the – are – are they asking
about, um, getting the actual image back from the camera or
getting a stream
from the camera so it's like a…
I'm – I'm not too sure.
Uh, maybe… Yeah… Maybe – maybe the last question.
Yeah, it
if – the latter you can't, Chris is correct, but I mean obviously you can
get the actual –
you can get the actual Bitmap coming out of the camera. Yeah.
And – and we have question is is that an internet access layer,
um, for calling remote services?
Not currently.
Yeah, okay. If if – can I ask, um, maybe. Yeah.
Yeah. Not currently.
Okay, well I – I think that's we have time for, uh, with Mike today. I just want to thank everyone for
coming again and – and,
uh, to leave your feedback on these seminars, uh, it's much appreciated.
Uh, we have
another community speaker coming up, um, in
two weeks.
He's just going to be talking about how you can style your iOS applications using
UI appearance and also some other, uh, hints and tips as well. So, uh, that should be, uh,
an exciting one with, uh, Nick Wise in two weeks.
Um, I hope you had a good time and uh, have a good rest of your day, thank you.