MAUI

.Net MAUI : Mobile local Storage Preferences using .NET MAUI

by Wednesday, June 22, 2022 comments
.NET MAUI provides 4 techniques for local storage options for storing data in locally on a device, depending on the nature, structure, and size of the data. The most used following 4 ways to store the local data on mobile devices.
  • Preferences
  • Local File Storage
  • Local Database
  • Secure Storage.
In this article, will share about the .Net MAUI Preference storage technique. Preferences is stores data in key-value pairs and can be easily managed via the Preferences class from Microsoft.Maui.Storage namespace and if you do Uninstall the app will also remove all local preferences, but if you are close and open the application still local preference can able to retrieve the old data.

.NET Multi-platform App UI (.NET MAUI) | .NET

The Preferences class stores preferences in the native local storage, more specifically
  • For Android, in the SharedPreferences.
  • For iOS, in the NSUserDefaults.
  • For UWP, in the ApplicationDataContainer.
On each platform, preferences are key/value pairs. The value can be of one of the primitive .NET types. Before understanding how preferences work, it is important to list the methods exposed by the Preferences class

Where to save .Net MAUI user settings

Create a new MAUI Application

You can open visual studio 2022 from your Windows or Mac machine. You must follow the below 3 steps to create a new MAUI application

Step 1: Select Create a new project

Step 2: Search the MAUI project template or choose Project Type > MAUI from the drop-down.

Step 3: Provide the configuration Details as a project name, Location, and Solutions name.

NET MAUI - app data storage options

Save Preferences

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
  • Single
  • Int64
  • String
  • DateTime
Preferences are set by calling the Preferences. Set method, providing the key and value

 Preferences.Set(“KEY”,” Value”).


Get/ Default Preferences

In this example already saved the two-preference value, 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.

Preferences.Get("Existing preferences key", Default Value);

The following code confirmed already saved preference key is available or not


Clear Preferences


Clear and remove will use for dropping the preference key and value, suppose if you are doing any log out or switching to a different user this will help to clear all the preference key and value.

Preferences.Clear();

Suppose if you want to remove a particular key preference in the mobile, you can use remove preference as like below

Preferences.Remove(“Name”);


Summary

The Preferences class is intended to store small pieces of information represented by primitive .NET types. If you need to locally save and retrieve more complex and structured data, a good option is to use a local database like SQL lite and you can refer below screen output for the preference sample application.

Screen 1: Show the first screen and display the default Date Time preference value in the label

Screen 2: After clicking on Register store the user name and the latest time in the preference

Screen 3: Display the existing preference value, if the user clicks on the logout option, the preference value becomes clear.

Building Mobile Apps with .NET MAUI
SJ
Suthahar Jegatheesan

Senior Solutions Architect · Microsoft Azure, AI & Mobile. Author of the Xamarin Q&A Book.

No comments:

sponsored

What Colleagues Say

“Suthahar is one of the most thorough and knowledgeable cloud architects I’ve worked with. His depth on Azure and ability to make complex mobile solutions work cross-platform is remarkable. He always delivers beyond expectations.”

AT Arun T. Senior Engineering Manager · Direct Manager

“I’ve followed Suthahar’s MSDEVBUILD blog for years — practical, deep, always current. Working alongside him confirmed what I suspected: he’s as sharp in person as on paper, and a genuinely generous mentor and architect.”

RK Rajesh K. Lead Developer · Mindtree

Certifications & Recognition

AZ-300 · Azure Architect Technologies AZ-301 · Azure Architect Design AZ-104 · Azure Administrator Associate AI-100 · Designing Azure AI Solutions DP-900 · Azure Data Fundamentals MCPD · Windows Phone Certified SAFe® 4 Scrum Master

Technical Expertise

☁ Cloud Platforms Microsoft Azure — Expert · 18 yrs App Services · AKS · Functions · Cosmos DB · APIM · Entra ID AWS & Google Cloud — Advanced
๐Ÿค– AI & Automation Azure OpenAI / GenAI — Advanced · 5 yrs RAG Pipelines · Bot Framework · Cognitive Services · Vision & Speech AI · ML.NET
๐Ÿ“ฑ Mobile Architecture .NET MAUI & Xamarin — Expert · 12 yrs Flutter · Native iOS/Android · Offline-First · Secure Storage & Auth
๐Ÿ”’ Identity & Security Microsoft Entra ID — Expert OAuth 2.0 · OIDC · Zero Trust · Conditional Access · RBAC · Key Vault

Speaking & Community

Microsoft User Group Malaysia Recurring speaker on Azure architecture, AI integration and .NET mobile development across Malaysia and APAC.
Global Azure Bootcamp — Chennai Presented “AI into Xamarin Mobile Apps” at GAB Chennai, part of the worldwide Global Azure community event.
AI & Azure OpenAI Community Events Workshops and live demos on RAG pipelines, AI agents and intelligent workflow automation since 2023.
Mobile & Cloud Workshops — Coimbatore Multi-day deep-dive sessions at GRG Computer Technology, KG Microsoft Innovation Center and RVS College of Arts and Science.
500+Blog Articles
50+Conference Talks
10K+Monthly Readers
30Public Repositories
3Continents Delivered
MSDEVBUILD Assistant Ask about Suthahar, Azure, AI or mentorship
Hi! I’m a simple FAQ bot for MSDEVBUILD. Tap a question below or type your own.