最好的方式来绑定Windows窗体属性的applicationSettings在C#中?

人气:1,057 发布:2022-09-10 标签: .net c#

问题描述

在一个桌面应用程序需要一些严重的重构,我有code几个块中,看起来是这样的:

In a desktop application needing some serious re-factoring, I have several chunks of code that look like this:

private void LoadSettings()
{
    WindowState = Properties.Settings.Default.WindowState;
    Location = Properties.Settings.Default.WindowLocation;
    ...
}

private void SaveSettings()
{
    Properties.Settings.Default.WindowState = WindowState;
    Properties.Settings.Default.WindowLocation = Location;
    ...
}

什么是替代这一目标的最佳方式是什么?项目施加的限制:

What's the best way to replace this? Project-imposed constraints:

在Visual Studio 2005中 在C#/。NET 2.0 Windows窗体

更新

由于Tundey,看起来像一段路要走。对于后人,我还发现了两个有用的教程:Windows窗体中的用户设置C#和探索持久性应用程序设置的秘密

Thanks Tundey, that looks like the way to go. For posterity, I've also found two useful tutorials: "Windows Forms User Settings in C#" and "Exploring Secrets of Persistent Application Settings".

我问了一下使用这种技术来绑定表单的大小here.我分离出来,以帮助人们谁寻找类似的问题。的

I've asked a follow-up question about using this technique to bind a form's Size here. I separated them out to help people who search for similar issues.

推荐答案

如果你打开​​的窗口中的设计形式,查看属性框中。第一项应为(ApplicationSetting)。根据该是(PropertyBinding)。这就是你会发现做的正是你想要的选项。

If you open your windows form in the designer, look in the properties box. The first item should be "(ApplicationSetting)". Under that is "(PropertyBinding)". That's where you'll find the option to do exactly what you want.

497