13

In Windows Forms and WPF applications we could use the Settings designer file to save user-specific settings. The docs only mention .Net Framework (e.g. 1 - 4), not .Net Core or .Net (e.g. 5+). I've confirmed that the .settings format and designer still works in .Net 6 WPF on Windows with Visual Studio 2022 though.

My questions is, is the .settings file format and designer still the recommended approach for .Net MAUI applications? Will it work across all platforms (Windows, macOS, mobile)? If not, is the recommended approach to simply write your own custom file for saving your user settings, and if so, where should the file be written to? Environment.SpecialFolder.ApplicationData / Environment.SpecialFolder.LocalApplicationData, or somewhere else?

6
  • Maybe Gerald will show up with up-to-date info. Barring that, this would be good to open as an issue at github.com/dotnet/maui. The WinForms and WPF approach I think is Windows specific (though I could be wrong). The solution is likely to be similar to what is done for Xamarin Forms. google xamarin forms user and configuration settings. Jan 5, 2022 at 20:59
  • Thanks @ToolmakerSteve. I did find an example of writing to a temp file on the xamarin-forms-samples here, but that's not necessarily the same as user settings. I found the Xamarin configuration settings you mentioned. Would be good to know if that will be the way to go with MAUI or not.
    – deadlydog
    Jan 5, 2022 at 21:11
  • Also, user's preferences can be saved with Xamarin Essentials Preferences. I'm certain that will be in Maui Essentials (if it isn't already). Jan 5, 2022 at 21:45
  • Hmm. Maybe app config becomes .net 6's responsibility. Then it could be a single solution, for all cross-platform scenarios, not just MAUI. This article about ConfigurationManager looks interesting. Jan 5, 2022 at 21:48
  • Yeah, I think user settings and app config are different beasts with different purposes, and my question is a bit confusing because I mention both. I'm going to edit it to only mention user settings, as that's what I meant to ask about.
    – deadlydog
    Jan 5, 2022 at 22:28

2 Answers 2

16

It all comes down to your requirements really. There is no silver bullet to this.

I must admit I didn't really know the settings (designer) file that you mentioned. I can tell that that's not a thing (as far as I know) in Xamarin or .NET MAUI. Probably also because there is the concept of a sandbox in mobile apps that is less a thing on desktop apps. So the location where things are saved are a bit more important.

In the comments there are already a couple of great suggestions to get you started. If your requirement is to just save simple types (string, bool, int, etc.) then the Essentials Preferences API should work just fine. I think under the hood that just serializes to a json file and stores it somewhere. Essentials is indeed already part of .NET MAUI at this time.

Xamarin.Forms used to have their own Properties, which is I think a more common concept in .NET applications, but I think the Essentials way is better.

If you want to save more complex things you might want to consider using a local database like SQLite or just roll your own and serialize your settings and save that somewhere, preferably with the Essentials FileSystem Helpers.

One thing to note here, and that is where the value of these libraries come in, is where you save the settings. Do you want the settings to be backed up to a cloud service so if survives a reinstall of the app? Or even an install of the same app on a new phone? Or do you then just want to reset the whole state and don't care about it.

If you figure that out, the rest is up to you! Hope this helps.

2
  • Thanks for the links Gerald. Good to know .settings is not supported in MAUI, and that using Xamarin.Essentials is the way to go over using the .Net native file path enums 👍 I assume the package name / namespace will change from Xamarin to something else down the road, like MAUI.Essentials, but I'll deal with that when/if it happens.
    – deadlydog
    Jan 6, 2022 at 15:52
  • 3
    .NET MAUI Essentials is already part of the previews and can be found under Microsoft.Maui.Essentials. Other than that namespace the APIs should be identical Jan 6, 2022 at 15:55
10

After a lot of struggling to find the right answer, I was able to use Preferences to really easily get/set user settings without having to worry about saving and loading myself.

// getter
var value = Preferences.Get("nameOfSetting", "defaultValueForSetting");

// setter
Preferences.Set("nameOfSetting", value);

I wrapped mine in a property so it's easier to use:

public string FilePath
{
    get { return Preferences.Get(nameof(FilePath), ""); }
    set { Preferences.Set(nameof(FilePath), value); }
}

It's not required but I used nameof instead of typing a literal string because I feel it's "safer" than using a random string for the key and hoping I remember it. Downside to this is if you change the property name it will change the key, so keep that in mind.

2
  • 1
    I feel like this could benefit from AOP with a [Preference] attribute
    – mattygs
    Feb 1 at 15:31
  • can you give an example of how to use the wrapped syntax? how to get/set Filepath?
    – canedha
    Jun 14 at 21:15

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.