>> On this week's Essential API of the Week,
I show you how to use preferences to save
settings information inside of your iOS,
Android, or Windows apps, so tune in.
[MUSIC]
>> Welcome back everyone to the Xamarin Show,
Essential API of the Week.
Today, I'm going to show you how to use preferences
which is one of my favorite APIs that we
have that enables you to save
a little tiny bits of data such as user preferences.
So, maybe remembering a state that was
on or a setting or even a feature flag in your application.
So, leveraging Xamarin essentials,
we can integrate this with just a few lines of code,
so let's jump in.
So, here I am inside of Visual Studio.
Now, I have a simple application
that I've been messing around with
that enables me to log in and I've added this Remember Me flag.
Now, of course, I need to remember information if I
actually plan to enable the "Remember Me" functionality.
For instance, perhaps, the next time I launch this application,
I save the state of
"Remember Me" of true and false and I can set that on startup.
So, let's take a look at the code really quick.
The code here is a simple Boolean is
RememberMe that it's simply just
returning the state true and false.
I look into the XAML in the code behind here.
I have a switch that is binding to that RememberMe state.
So, let us see how we can leverage
Xamarin Essentials to integrate this into our app.
So, the first thing I'm going to do is start on
docs.microsoft.com in Xamarin Essentials.
There's a small getting started guide that you should
follow for a little bit of setup code in your iOS,
Android, and Windows application,
and then all of the feature guides.
So here, we can scroll down and tap on "Preferences" that allow
me to quickly and easily add persisted preferences into the app.
It's really easy to get started.
There's no additional setup code that you
need additionally from that getting started guide,
and here's how we do it.
All I have to do is add the using Xamarin essentials namespace,
add Set or Get to get my preference.
You can store all sorts of preferences such as strings,
Booleans, DdateTime, long, strings right
into the preferences class.
Now, this is for small bits of information,
so you don't want to store a whole database,
but just a few things.
So, the key here is that you say Get with
a persisted key string and
a default value that you want to get back,
and of course, you can set it by adding that same key,
and then the value you want.
You can always remove it or remove all preferences.
So, let's go ahead and integrate it into the app.
The first thing that I've done is I've ensured that I've
installed Xamarin Essentials into my app.
Now, I'm using Xamarin Forms here, but you don't have to,
no matter any iOS,
Android, or Windows app, you can use Xamarin Essentials.
So, the first thing we're going to do like I said in the dock,
is to go in and add
Xamarin.Essentials into the namespace. All right.
So next, all we got to do down
here for this is come in and say, "All right,
well, the preferences instead of storing this value around,
there's really no need for it."
What we could do is simply remove that and say,
whenever I want to get it,
say preferences.get, now we need a key,
so I'll say RememberMe here,
and then I'll give it a default value.
So, the default value is going to be false,
so we want that to be off by default.
Then, here what we can do is we can say preferences.set,
and I'll say RememberMe,
and I'll set it to the value,
and then we'll just raise the same
exact on property changed event.
Now, here's a cool thing to do: instead of hard-coding the string,
if you're using it in an auto property,
you can use this nameof,
and that will pass that same string down.
So here, for instance,
instead of just doing that RememberMe and hard coding that string,
I can put it there like that, nameof,
and then pass it the false or the value to get or set.
Now, you don't have to use name of,
you can also use the string or a constant string,
and put all of your preferences into a separate file.
You can use it wherever you're at.
So, now let's go ahead and relaunch this application.
Instead of using that local property for that default value,
we should see false here as the default.
So, let's go ahead and let her redeploy.
There we go. So, I can go ahead and log in,
or I can toggle on the "Remember Me" state.
Now, as soon as I toggle that,
that's going to set the preference
and raise a notification if needed.
So now, if I go ahead and close out
this application and bring it up again,
"AppEssentials", this is going to relaunch the application,
but it's going to have the same state that
we set on the preferences of RememberMe.
So, now, it's on by default,
I just persisted that setting in
the application with a few lines of code,
and underlying it's going to call the correct iOS,
Android, or Windows API no matter where I run this application.
So, there you have it,
we've integrated preferences into
our application with just a few lines of code.
What's really nice here is that you can use
this throughout your entire application,
your iOS, Android, Windows app, shared code,
enable you to persist your different settings and state across
application updates and different launch events.
So, there you have it. Hope you enjoyed
this Essential API of the Week.
Don't forget to subscribe because we have more coming out each
and every week right here on YouTube or Channel nine.
So, thanks for watching and I'll see you next time.
[MUSIC]