Reputation: 24424
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?
Upvotes: 18
Views: 16704
Reputation: 330
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.
Upvotes: 16
Reputation: 34103
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.
Upvotes: 20