简体   繁体   English

(ApplicationSettings)未显示在表单属性上 - Visual Studio 2022 C#

[英](ApplicationSettings) not showing on form properties - Visual Studio 2022 C#

I was looking for a way to create a sort of "global" setting for the background color of some forms in a project.我正在寻找一种方法来为项目中某些 forms 的背景颜色创建一种“全局”设置。 Some examples I found suggested using this (ApplicationSettings) option on Properties, like this (sorry I'm new here so I can't post the image directly):我发现一些示例建议在属性上使用此 (ApplicationSettings) 选项,例如(对不起,我是新来的,所以我不能直接发布图像):

在此处输入图像描述

The problem is that I looked everywhere and couldn't find that, mine appears like this:问题是我到处找都找不到,我的看起来像这样:

在此处输入图像描述

Is there a way to add that option?有没有办法添加该选项?

That option is available for projects targeting .NET Framework but not for those targeting .NET Core, which includes .NET 5 and later.该选项适用于针对 .NET 框架的项目,但不适用于针对 .NET 核心的项目,其中包括 .NET 5 及更高版本。 Windows Forms was not originally supposed to make the trip to .NET Core but Microsoft realised that many people would sooner ignore .NET Core than abandon WinForms. Windows Forms 原本不应该前往 .NET 核心,但微软意识到许多人会更早地忽略 Z303CB0EF59EDB9082D6 核心而不是放弃。 You have a choice to make whether you want that functionality or you want to target .NET Core.您可以选择是要该功能还是要针对 .NET Core。

Note that you can probably just write the code yourself to bind a property to an application setting.请注意,您可能只需要自己编写代码来将属性绑定到应用程序设置。 I just created a binding for the Text property of a form and the following code was generated in the designer code file:我刚刚为表单的Text属性创建了一个绑定,并且在设计器代码文件中生成了以下代码:

this.DataBindings.Add(new System.Windows.Forms.Binding("Text", global::MyWindowsFormsApp.Properties.Settings.Default, "Form1Text", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));

You can basically write that code yourself in the Load event handler, where you would likely bind other properties, eg to data retrieved from a database.您基本上可以在Load事件处理程序中自己编写该代码,您可能会在其中绑定其他属性,例如从数据库检索的数据。

The ability to bind directly to application settings in Windows Forms has not been migrated from the .NET Framework to .NET Core.直接绑定到 Windows Forms 中的应用程序设置的能力尚未从 .NET 框架迁移到 Z303CB0EF58EDB99872D61 核心。

However, with some code and a workaround you can still bind to your settings.但是,通过一些代码和解决方法,您仍然可以绑定到您的设置。

  1. Right click on you project and navigate to Properties > Settings > General .右键单击您的项目并导航到Properties > Settings > General Click on "Create or open application settings" .单击“创建或打开应用程序设置”

  2. Add your setting, eg, with Name = "DefaultBackColor" and Type = System.Drawing.Color .添加您的设置,例如,使用Name = "DefaultBackColor"Type = System.Drawing.Color

  3. Close the project properties and compile.关闭项目属性并编译。

  4. Visual Studio has created a file Properties > Settings.settings > Settings.Designer.cs . Visual Studio 创建了一个文件Properties > Settings.settings > Settings.Designer.cs Open Settings.Designer.cs .打开Settings.Designer.cs You will see an automatically generated internal sealed partial class Settings having a property public Color DefaultBackColor { get; }您将看到一个自动生成的internal sealed partial class Settings具有一个属性public Color DefaultBackColor { get; } public Color DefaultBackColor { get; } . public Color DefaultBackColor { get; } To be able to create a binding source we must make this class public and compile again.为了能够创建绑定源,我们必须将此 class public并再次编译。 This class will be re-created later with the internal modifier, but we do not mind, as we need the public modifier only temporarily.这个 class 稍后将使用internal修饰符重新创建,但我们不介意,因为我们只是暂时需要public修饰符。

  5. In the Forms designer click on the background of your form to select it in the properties window.在 Forms 设计器中单击您表单的背景到 select 它在属性 window 中。

  6. In the properties window select DataBindings > Advanced .在属性 window select DataBindings > Advanced中。 In the Bindings combo box select "Add new Object Data Source..." , select the Settings class and click OK . In the Bindings combo box select "Add new Object Data Source..." , select the Settings class and click OK .

  7. In my case Visual Studio did not update the Binding drop-down immediately.在我的情况下,Visual Studio 没有立即更新绑定下拉菜单。 So, I had to fiddle around a bit, but finally I got the entry Other Data Sources > Project Data Sources > Properties > Settings > DefaultBackColor .所以,我不得不摆弄一下,但最后我得到了条目Other Data Sources > Project Data Sources > Properties > Settings > DefaultBackColor In the left list select BackColor and then select this DefaultBackColor in the Binding drop-down.在左侧列表 select BackColor然后 select 这个DefaultBackColor在 Binding 下拉列表中。 This creates a settingsBindingSource on your form and wires up the BackColor of your form.这会在您的表单上创建一个settingsBindingSource并连接表单的BackColor

  8. Set the data source of this binding source in the constructor of your form:在表单的构造函数中设置此绑定源的数据源:

     public Form1() { InitializeComponent(); settingsBindingSource.DataSource = Properties.Settings.Default; }

Now, the form and all the controls, which inherit the BackColor from the form, will have the color defined in the application settings.现在,从窗体继承BackColor的窗体和所有控件将具有在应用程序设置中定义的颜色。

In your other forms repeat the steps 7 and 8.在您的其他 forms 中重复步骤 7 和 8。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM