F#为WPF应用程序保留一些用户值

Gos*_*win 2 wpf f#

在我的小型WPF应用程序(仅限F#)中,我想在关闭后记住窗口大小和位置.此C#解决方案建议使用项目设置IDictinary for User.config.这看起来像我之后的简单方法,但我没有在我的F#项目中找到项目设置.它们是否存在于F#项目中?

我试过这个但它不起作用:(在C#示例中的save()调用不可用.)

let getSetting k def = 
    if Application.Current.Properties.Contains k 
       then Application.Current.Properties.Item k 
       else def

let window = System.Windows.Window()
// is box() the best wax to make floats into obj ?
window.Top <-     getSetting "WindowTop"    (box 0.0) |> unbox  
window.Left <-    getSetting "WindowLeft"   (box 0.0) |> unbox 
window.Height <-  getSetting "WindowHeight" (box 800.0) |> unbox  
window.Width <-   getSetting "WindowWidth"  (box 800.0) |> unbox

window.Closing.Add( fun _ -> 
      Application.Current.Properties.Add("WindowTop",window.Top)
      Application.Current.Properties.Add("WindowHeight",window.Height)
      Application.Current.Properties.Add("WindowLeft",window.Left)
      Application.Current.Properties.Add("WindowWidth",window.Width)
      //Application.Current.Properties.Save() // not available!
      )
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用类型提供程序,但我想保持简单,如果可能的话没有依赖关系.是否有内置方法可以在F#WPF应用程序中保留一些用户值?

Fun*_*unk 5

正如@ mm8指出的那样,您提到的方法取决于C#WPF应用程序的项目模板中包含的Settings.settings文件.

C#WPF应用程序

F#模板不提供这样的功能,这是不幸的,因为XAML支持非常酷.相反,您可以使用App.config文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  ...

  <appSettings>
    <add key="WindowTop" value="0" />
    <add key="WindowLeft" value="0" />
    <add key="WindowHeight" value="350" />
    <add key="WindowWidth" value="525" />
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

如果您不想依赖FSharp.Configuration.dll,可以使用ConfigurationManager(注意您仍需要添加对System.Configuration.dll的引用).

open System.Configuration

type UserSettings = {
    WindowTop    : float
    WindowLeft   : float
    WindowHeight : float
    WindowWidth  : float
  } with 
    static member Load() =
      let config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
      { // To do: add validation
        WindowTop    = float config.AppSettings.Settings.["WindowTop"].Value 
        WindowLeft   = float config.AppSettings.Settings.["WindowLeft"].Value
        WindowHeight = float config.AppSettings.Settings.["WindowHeight"].Value
        WindowWidth  = float config.AppSettings.Settings.["WindowWidth"].Value
      }
    member this.Save() =
      let config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
      config.AppSettings.Settings.["WindowTop"].Value     <- string this.WindowTop
      config.AppSettings.Settings.["WindowLeft"].Value    <- string this.WindowLeft
      config.AppSettings.Settings.["WindowHeight"].Value  <- string this.WindowHeight
      config.AppSettings.Settings.["WindowWidth"].Value   <- string this.WindowWidth
      config.Save() 
Run Code Online (Sandbox Code Playgroud)

现在你可以:

open System.Windows

let window = new Window()    
let settings = UserSettings.Load()

window.Top    <- settings.WindowTop
window.Left   <- settings.WindowLeft
window.Height <- settings.WindowHeight
window.Width  <- settings.WindowWidth

window.Closing.Add( fun _ -> 
  {
    WindowTop    = window.Top
    WindowLeft   = window.Left
    WindowHeight = window.Height
    WindowWidth  = window.Width
  }.Save() )
Run Code Online (Sandbox Code Playgroud)