❔ How do i handle user settings in wpf .net core

How do i handle user settings in WPF .NET 6?
14 Replies
Pobiega
Pobiega2y ago
Define "user settings"? M.E.C doesnt support two-way config updates very well, so it might not be ideal for that usecase.
ricecracker2234
ricecracker2234OP2y ago
basically settings that the end-users can change
Pobiega
Pobiega2y ago
by editing the config file manually, or through some WPF interface in your app?
ricecracker2234
ricecracker2234OP2y ago
through the wpf interface with bindings
Pobiega
Pobiega2y ago
Right. Thats unfortunately not the best usecase for M.E.C afaik
ricecracker2234
ricecracker2234OP2y ago
oh
Pobiega
Pobiega2y ago
it has good support for reloading the file as it changes, but it doesnt have two-way binding support
ricecracker2234
ricecracker2234OP2y ago
so what's a good library for handling user settings in general?
Pobiega
Pobiega2y ago
Whats wrong with System.Configuration? it supports two-way binding
ricecracker2234
ricecracker2234OP2y ago
does it support other types like enums?
Pobiega
Pobiega2y ago
I have no idea. Lets try. can confirm, works fine with enums strings, ints are all fine too it gets xml serialized by default
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="TestSettings" type="WpfApp1.TestSettings, WpfApp1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>
<TestSettings OptIn="HellYes" />
</configuration>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="TestSettings" type="WpfApp1.TestSettings, WpfApp1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>
<TestSettings OptIn="HellYes" />
</configuration>
public class TestSettings : ConfigurationSection
{
public const string SectionName = nameof(TestSettings);

[ConfigurationProperty(nameof(OptIn), DefaultValue = CoolEnum.Unknown)]
public CoolEnum OptIn
{
get => (CoolEnum)this[nameof(OptIn)];
set => this[nameof(OptIn)] = value;
}
}

public enum CoolEnum
{
Unknown,
HellYes,
PleaseNo
}
public class TestSettings : ConfigurationSection
{
public const string SectionName = nameof(TestSettings);

[ConfigurationProperty(nameof(OptIn), DefaultValue = CoolEnum.Unknown)]
public CoolEnum OptIn
{
get => (CoolEnum)this[nameof(OptIn)];
set => this[nameof(OptIn)] = value;
}
}

public enum CoolEnum
{
Unknown,
HellYes,
PleaseNo
}
I made a shitty window that has a TestSettings instance (added and fetched from the Configuration object made by ConfigurationManager.OpenExeConfiguration) set as its datacontext and bound it there. Works fine setting values, changing values, restarting the program etc.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Pobiega
Pobiega2y ago
Thats fair. Set up a simple POCO object for your config, add some Load/Save methods and you're done thats probably a lot easier 😄
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server