Better Forms – Remember Entries with Xam.Plugins.Settings

In this lesson

  • Add Xam.Plugins.Settings NuGet package
  • Create Settings class
  • Add and retrieve previous entry via getter


Tap on time to skip ahead


One of the things that we can do to improve our user’s experience when working with our Xamarin.Forms applications is to find ways to keep data entry to a minimum. For example, with a lot of login forms, we require users to type in their email address. But, if I leave the application and force quit the application or when I restart the app, I get a new instance of the app, it would be nice if we could remember what the user has typed in for their email. So, how do we go about doing this? We use a nuget package named Xam.Plugins.Settings.


One of the first things I’m going to do is go and see if there is anything already existing on NuGet. And, I actually do know of something out there. It is a plugin that was built at Xamarin. I’m going to call Xamarin Settings, we’re going to scroll down, here we go. Xam.Plugins.Settings, Plugin for Xamarin and Windows by James Montemagno. I’m going to add it to my PCL. Then, I also need to make sure I add Xam.Plugins.Settings to my iOS and Android projects as well. Luckily Xamarin Studio has remembered what we’ve done previously.


If we take a look here, we’ll see that James has included this very handy Settings class here. It kind of looks a little funky but I’m just going to go ahead and copy and paste from here. I’m going to create a new package called Utils. I’m going to say Add, New File. We’re just going to do an empty class called Settings. Do select all, delete and paste. Then, it will update our namespace here. We’re going to say BetterForms.Utils. We get this nice Intellisense where it tells us, “Hey, the namespace should just be Utils.”


We’ll see here that we have a property called GeneralSettings. Let’s change this property name to LastUsedEmail. You’ll see here, the app settings, we kind of have just your standard Key Pair Value Storage API. We can see we have this settings key here. This key is basically just a string that is a unique ID of this particular thing that we are storing in the settings of our device. I’m going to change this to LastEmailSettingsKey. And I’m going to change this to LastEmailKey. Now, all I have to do in our ViewModel is go into the Login ViewModel, in my setter of our email address, now if I say Settings.LastUsedEmail = value. Now, whenever our ViewModel is instantiated, we will say EmailAddress = Settings.LastUsedEmail.


Now, if we run our application, it will still look the same because I haven’t entered anything yet. If I type in my email address, if I leave the application, force quit it, when I come back, my email field should be pre-populated and it is. This is how you can real easily and quickly save what your users have put in previously in any sort of form field that requires repetitive entry.