How to implement appSettings in MVVM
<p>I'm trying to get rid of a warning I get using <code>appSettings</code> in a WPF project.</p>

<p><strong>App.config</strong></p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;configuration&gt;

  ...

  &lt;appSettings&gt;
    &lt;add key="Language" value="English" /&gt;
    &lt;add key="Department" value="Production" /&gt;
  &lt;/appSettings&gt;
&lt;/configuration&gt;
</code></pre>

<p><strong>Model</strong></p>

<pre><code>open System.Configuration

type UserSettings = {
    Language    : string
    Department  : string
} with 
    static member Null =
        {
        Language    = "English"
        Department  = "Production"
        }
    static member Load() =
        let config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
        {
        Language    = config.AppSettings.Settings.["Language"].Value
        Department  = config.AppSettings.Settings.["Department"].Value
        }
    member this.Save() =
        let config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
        config.AppSettings.Settings.["Language"].Value   &lt;- this.Language
        config.AppSettings.Settings.["Department"].Value &lt;- this.Department
        config.Save() 
</code></pre>

<p><strong>ViewModel</strong></p>

<pre><code>open FSharp.ViewModule

type SettingsViewModel() as self = 
    inherit ViewModelBase()    

    let settings    = UserSettings.Load()

    let department  = self.Factory.Backing(&lt;@ self.Department @&gt;, settings.Department)

    ...
</code></pre>

<p><strong>View</strong></p>

<pre><code>&lt;Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ViewModels;assembly=mySolution"
    xmlns:fsxaml="http://github.com/fsprojects/FsXaml"
    Title="{Binding Department}" Height="300" Width="400"&gt;
    &lt;Window.DataContext&gt;
        &lt;local:SettingsViewModel/&gt;
    &lt;/Window.DataContext&gt;

    ...

&lt;/Window&gt;
</code></pre>

<p>The visual designer marks the line <code>&lt;local:SettingsViewModel/&gt;</code> and gives a warning:</p>

<blockquote>
  <p>Object reference not set to an instance of a reference.</p>
</blockquote>

<p>This seems to be caused by the order in which the individual layers (MVVM) are initiated.</p>

<p>Changing</p>

<pre><code>let settings    = UserSettings.Load()
</code></pre>

<p>to</p>

<pre><code>let settings    = UserSettings.Null
</code></pre>

<p>resolves the warning.</p>

<p><em>Second approach</em></p>

<pre><code>open FSharp.Configuration
type configSettings = AppSettings&lt;"app.config"&gt;
</code></pre>

<p>This time the line <code>&lt;local:SettingsViewModel/&gt;</code> gives an error (still builds):</p>

<blockquote>
  <p>Error 1 Cannot find name Language in 'appSettings' section of config
  file. (C:\Program Files (x86)\Microsoft Visual Studio
  12.0\Common7\IDE\XDesProc.exe.Config)</p>
</blockquote>

<p>Is there a way to implement <code>appSettings</code> that doesn't lead to warnings/errors?</p>

<p><strong>EDIT</strong></p>

<p>Layers used for the second approach:</p>

<p><strong>Model</strong></p>

<pre><code>open FSharp.Configuration
type UserSettings = AppSettings&lt;"app.config"&gt;
</code></pre>

<p><strong>ViewModel</strong></p>

<pre><code>open FSharp.ViewModule

type SettingsViewModel() as self = 
    inherit ViewModelBase()    

    let department  = self.Factory.Backing(&lt;@ self.Department @&gt;, UserSettings.Department)

    ...
</code></pre>