2017-08-11 60 views
2

在我的小WPF應用程序(僅限F#)中,我想記住關閉後的窗口大小和位置。 This C# solution建議使用User.config的項目設置IDictinary。這看起來像我之後的簡單方法,但我沒有在我的F#項目中找到項目設置。它們是否適用於F#項目?F#爲WPF應用程序保留了一些用戶值

我試過,但它不工作:(保存()調用如C#示例不可用。)

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! 
    ) 

我知道我可以使用一個type provider但我想保持它如果可能的話,簡單且沒有依賴性。是否有內置的方式來保留F#WPF應用程序中的某些用戶值?

+1

Properties.Settings.Default指這是從WPF應用程序的項目模板中包含的Properties-> Setting.settings文件生成的Settings類。 – mm8

回答

5

正如@ mm8指出的那樣,您提到的方法取決於包含在C#WPF應用程序的項目模板中的Settings.settings文件。

C# WPF App

的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> 

如果你不想依賴於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() 

現在,您可以:使用System.Configuration.dll

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()) 
0

感謝我想出了這個作爲最小的通解芬克的答案:

module Config = 
    open System.Configuration 
    let private config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) 

    let save key value = 
     try config.AppSettings.Settings.[key].Value <- value 
     with _ -> config.AppSettings.Settings.Add(key,value) // in case key does not exist yet 
     config.Save() 

    let load key = 
     try Some config.AppSettings.Settings.[key].Value 
     with _ -> None 
相關問題