Equivalent to UserSettings / ApplicationSettings in WPF dotnet core

前端 未结 6 1325
没有蜡笔的小新
没有蜡笔的小新 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:51

    For Wpf Net.Core

    Project click Right Mouse Button -> Add New Item -> Settings File (General)

    Use

    Settings1.Default.Height = this.Height;
    Settings1.Default.Width = this.Width;
    
    this.Height = Settings1.Default.Height;
    this.Width = Settings1.Default.Width;
    
    Settings1.Default.Save();
    

    Where 'Settings1' created file name

    EXAMPLE

    Double click 'Settings1.settings' file and Edit

    private void MainWindowRoot_SourceInitialized(object sender, EventArgs e)
    {
        this.Top = Settings1.Default.Top;
        this.Left = Settings1.Default.Left;
        this.Height = Settings1.Default.Height;
        this.Width = Settings1.Default.Width;
        // Very quick and dirty - but it does the job
        if (Settings1.Default.Maximized)
        {
            WindowState = WindowState.Maximized;
        }
    }
    
    private void MainWindowRoot_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        if (WindowState == WindowState.Maximized)
        {
            // Use the RestoreBounds as the current values will be 0, 0 and the size of the screen
            Settings1.Default.Top = RestoreBounds.Top;
            Settings1.Default.Left = RestoreBounds.Left;
            Settings1.Default.Height = RestoreBounds.Height;
            Settings1.Default.Width = RestoreBounds.Width;
            Settings1.Default.Maximized = true;
        }
        else
        {
            Settings1.Default.Top = this.Top;
            Settings1.Default.Left = this.Left;
            Settings1.Default.Height = this.Height;
            Settings1.Default.Width = this.Width;
            Settings1.Default.Maximized = false;
        }
    
        Settings1.Default.Save();
    }
    

提交回复
热议问题