Best practices for storing UI settings?

前端 未结 7 1664
忘掉有多难
忘掉有多难 2020-12-13 21:49

we\'re currently planning a larger WPF LoB application and i wonder what others think being the best practice for storing lots of UI settings e.g.

  • Expander Sta
7条回答
  •  有刺的猬
    2020-12-13 22:34

    Digging into aogan's answer and combining it with decasteljau's answer and the blog post he referenced, here is an example that fills in some gaps that weren't clear to me.

    The xaml file:

    And the source file:

    namespace MyApp
    {
        class MainWindow ....
        {
            ...
    
            protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
            {
                MyAppSettings.Default.Save();
                base.OnClosing(e);
            }
        }
    
        public sealed class MyAppSettings : System.Configuration.ApplicationSettingsBase
        {
            private static MyAppSettings defaultInstance = ((MyAppSettings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new MyAppSettings())));
            public static MyAppSettings Default
            {
                get { return defaultInstance; }
            }
    
            [System.Configuration.UserScopedSettingAttribute()]
            [System.Configuration.DefaultSettingValueAttribute("540")]
            public int MainWndHeight
            {
                get { return (int)this["MainWndHeight"]; }
                set { this["MainWndHeight"] = value; }
            }
    
            [System.Configuration.UserScopedSettingAttribute()]
            [System.Configuration.DefaultSettingValueAttribute("790")]
            public int MainWndWidth
            {
                get { return (int)this["MainWndWidth"]; }
                set { this["MainWndWidth"] = value; }
            }
    
            [System.Configuration.UserScopedSettingAttribute()]
            [System.Configuration.DefaultSettingValueAttribute("300")]
            public int MainWndTop
            {
                get { return (int)this["MainWndTop"]; }
                set { this["MainWndTop"] = value; }
            }
    
            [System.Configuration.UserScopedSettingAttribute()]
            [System.Configuration.DefaultSettingValueAttribute("300")]
            public int MainWndLeft
            {
                get { return (int)this["MainWndLeft"]; }
                set { this["MainWndLeft"] = value; }
            }
        }
    }
    

提交回复
热议问题