In this tutorial, we will see how to use the local storage preferences in the .Net MAUI application. Preferences is stores data i...

.Net MAUI - Local Storage Preferences .Net MAUI - Local Storage Preferences

.Net MAUI - Local Storage Preferences

.Net MAUI - Local Storage Preferences

In this tutorial, we will see how to use the local storage preferences in the .Net MAUI application. Preferences is stores data in key-value pairs and can be easily managed via the Preferences class from Microsoft.Maui.Storage namespace.

Note:If we uninstall the app, will remove all local preferences.

Platform differences

Preferences are stored natively, which allows you to integrate your settings into the native system settings.

  • Android:All data is stored into Shared Preferences. If no sharedName is specified, the default Shared Preferences are used. Otherwise, the name is used to get a private Shared Preferences with the specified name.
  • iOS/macOS:NSUserDefaults is used to store values on iOS devices. If no sharedName is specified, the StandardUserDefaults are used. Otherwise, the name is used to create a new NSUserDefaults with the specified name used for the NSUserDefaultsType.SuiteName.
  • Windows:ApplicationDataContainer is used to store the values on the device. If no sharedName is specified, the LocalSettings are used. Otherwise the name is used to create a new container inside of LocalSettings. LocalSettings restricts the preference key names to 255 characters or less. Each preference value can be up to 8K bytes in size, and each composite setting can be up to 64 K bytes in size.

Quick Links:


Project Setup:

  • Launch Visual Studio 2022, and in the start window click Create a new project to create a new project.
  • In the Create a new project window, select MAUI in the All project types drop-down, select the .NET MAUI App template, and click the Next button:
  • In the configure your new project window, name your project, choose a suitable location for it, and click the Next button:
  • In the Additional information window, click the Create button:
  • Once the project is created, we can able to see the Android, iOS, Windows and other running options in the toolbar. Press the emulator or run button to build and run the app

Implementation:

    Save/Set Preference will store simple value, preference having different key and value, preference key always string type, the value of preference must be one of the following types.

  • Boolean
  • Double
  • Int32
  • Int64
  • Single
  • String
  • DateTime
  • In the below statement, will the get the value from existing preference or if preference value is not there automatic or return the default value.

    For dropping preference key and value, clear and remove are used. For example, if you are switching to a different user or logging out, this will assist in clearing all preference key and value. We can use remove preference to get rid of a specific key preference in a mobile device.

  • In Solution Explorer, click MainPage.xaml and replace the page content with what is shown below:
  • Open the MainPage.xaml.cs file and the page content as shown below:

Full Code:

Download Code:

You can download the code from GitHub. If you have any doubts, feel free to post a comment. If you liked this article, and it is useful to you, do like, share the article & star the repository on GitHub.

0 comments:

Please Comment about the Posts and Blog