Best practices for storing UI settings?

前端 未结 7 1666
忘掉有多难
忘掉有多难 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:24

    We store the preferences file here:

    Environment.SpecialFolder.ApplicationData
    

    Store it as xml "preferences" file so it's not so hard to get to and change if it ever gets corrupted.

    So far this has worked much better than the registry for us, it's cleaner and easier to blow out if anything gets corrupted or needs to be reset.

    0 讨论(0)
  • 2020-12-13 22:28

    Seems to be losing popularity for some reason; but the registry has always been an appropriate place for these kinds of settings.

    0 讨论(0)
  • 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:

    <Window ...
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:p="clr-namespace:MyApp"
        Height="{Binding Source={x:Static p:MyAppSettings.Default}, Path=MainWndHeight, Mode=TwoWay}"
        Width="{Binding Source={x:Static p:MyAppSettings.Default}, Path=MainWndWidth, Mode=TwoWay}"
        Left="{Binding Source={x:Static p:MyAppSettings.Default}, Path=MainWndLeft, Mode=TwoWay}"
        Top="{Binding Source={x:Static p:MyAppSettings.Default}, Path=MainWndTop, Mode=TwoWay}"
        ...
    

    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; }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-13 22:34

    We use a custom SettingsProvider to store the config information in a table in the app's database. This is a good solution if you're already using a database.

    0 讨论(0)
  • 2020-12-13 22:36

    In the Programming WPF by Chris Sells & Ian Griffiths it says

    The preferred setting mechanism for WPF application is the one provided by .NET and VS: the ApplicationSettingBase class from the System.Configuration namespace with the built-in designer.

    0 讨论(0)
  • 2020-12-13 22:43

    The quicker way to store UI settings is using the Properties.Settings.Default system. What can be nice with it is to use WPF binding to the value. Example here. Settings are automatically updated and loaded.

    <Window ...
        xmlns:p="clr-namespace:UserSettings.Properties"
        Height="{Binding Source={x:Static p:Settings.Default}, Path=Height, Mode=TwoWay}" 
        Width="{Binding Source={x:Static p:Settings.Default}, Path=Width, Mode=TwoWay}" 
        Left="{Binding Source={x:Static p:Settings.Default}, Path=Left, Mode=TwoWay}" 
        Top="{Binding Source={x:Static p:Settings.Default}, Path=Top, Mode=TwoWay}">
    
    ...
    protected override void OnClosing(System.ComponentModel.CancelEventArgs e) 
    { 
        Settings.Default.Save(); 
        base.OnClosing(e); 
    }
    

    The problem with that is that it quickly becomes a mess if your application is large.

    Another solution (proposed by someone here) is to use the ApplicationData path to store your own preferences into XML. There you can build your own setting class and use the XML serializer to persist it. This approach enables you to do migration from versions to versions. While being more powerful, this method requires a bit more code.

    0 讨论(0)
提交回复
热议问题