将 Windows 窗体属性绑定到 C# 中的 ApplicationSettings 的最佳方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to bind Windows Forms properties to ApplicationSettings in C#?
提问by Brian Jorgensen
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 Forms
- 视觉工作室 2005
- C#/.NET 2.0
- Windows 窗体
Update
更新
For posterity, I've also found two useful tutorials: "Windows Forms User Settings in C#"and "Exploring Secrets of Persistent Application Settings".
对于后代,我还找到了两个有用的教程:“C# 中的 Windows 窗体用户设置”和“探索持久应用程序设置的秘密”。
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.-------------------
我在此处询问了有关使用此技术绑定表单大小的后续问题。我将它们分开以帮助寻找类似问题的人。-------------------
采纳答案by Tundey
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.
如果您在设计器中打开 Windows 窗体,请查看属性框。第一项应该是“(ApplicationSetting)”。下面是“(PropertyBinding)”。在这里您可以找到完全按照您的意愿行事的选项。