Equivalent to UserSettings / ApplicationSettings in WPF dotnet core

前端 未结 6 1321
没有蜡笔的小新
没有蜡笔的小新 2020-12-23 16:32

What is the prefered way for persisting user settings for WPF applications with .Net Core >=3.0?

Created WPF .Net Core 3.0 Project (VS2019 V16.3.1) Now I have seen t

6条回答
  •  一向
    一向 (楼主)
    2020-12-23 16:54

    As pointed out in the posts you referenced, the Microsoft.Extensions.Configuration API is meant as a one time set up for your app, or at the very least to be read-only. If you're main goal is to persist user settings easy/fast/simple, you could roll something up yourself. Storing the settings in the ApplicationData folder, in resemblance to the old API.

    public class SettingsManager where T : class
    {
        private readonly string _filePath;
    
        public SettingsManager(string fileName)
        {
            _filePath = GetLocalFilePath(fileName);
        }
    
        private string GetLocalFilePath(string fileName)
        {
            string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            return Path.Combine(appData, fileName);
        }
    
        public T LoadSettings() =>
            File.Exists(_filePath) ?
            JsonConvert.DeserializeObject(File.ReadAllText(_filePath)) :
            null;
    
        public void SaveSettings(T settings)
        {
            string json = JsonConvert.SerializeObject(settings);
            File.WriteAllText(_filePath, json);
        }
    }
    

    A demo using the most basic of UserSettings

    public class UserSettings
    {
        public string Name { get; set; }
    }
    

    I'm not going to provide a full MVVM example, still we'd have an instance in memory, ref _userSettings. Once you load settings, the demo will have its default properties overridden. In production, of course, you wouldn't provide default values on start up. It's just for the purpose of illustration.

    public partial class MainWindow : Window
    {
        private readonly SettingsManager _settingsManager;
        private UserSettings _userSettings;
    
        public MainWindow()
        {
            InitializeComponent();
    
            _userSettings = new UserSettings() { Name = "Funk" };
            _settingsManager = new SettingsManager("UserSettings.json");
        }
    
        private void Button_FromMemory(object sender, RoutedEventArgs e)
        {
            Apply(_userSettings);
        }
    
        private void Button_LoadSettings(object sender, RoutedEventArgs e)
        {
            _userSettings = _settingsManager.LoadSettings();
            Apply(_userSettings);
        }
    
        private void Button_SaveSettings(object sender, RoutedEventArgs e)
        {
            _userSettings.Name = textBox.Text;
            _settingsManager.SaveSettings(_userSettings);
        }
    
        private void Apply(UserSettings userSettings)
        {
            textBox.Text = userSettings?.Name ?? "No settings found";
        }
    }
    

    XAML

    
        
             
        
        
            
                
                
                
                
            
            
            
            
            
        
    
    

提交回复
热议问题