如何从自定义组件添加应用程序设置(WinForms)

时间:2021-09-09 00:11:35

I have a WinForms user control that, when added to a form, should automatically add some elements to application settings. (Of course, the user should be able to customize/disable this behavior.)

我有一个WinForms用户控件,当添加到表单时,应该自动添加一些元素到应用程序设置。 (当然,用户应该能够自定义/禁用此行为。)

is that advisable? What is the "good" way to do this?

那是可取的吗?这样做的“好”方法是什么?

[edit]
The control provides a default implementation for the file menu, the consumer only needs to wire up the menu/toolbar items in the designer, and implement some basic events. Part of this is a recent file list, which by default should be remembered.

[edit]控件提供了文件菜单的默认实现,消费者只需要连接设计器中的菜单/工具栏项,并实现一些基本事件。部分内容是最近的文件列表,默认情况下应记住该列表。

I agree that the consumer needs full control whether or not he wants these settings added automatically.

我同意消费者需要完全控制他是否想要自动添加这些设置。

So far, I expose the file list as a public string property, and the consumer can add the code to init and store this from/in the application config. If possible, I'd like to simplify this further so the consumer just provides the settings variable where he wants the setting to be stored (if he wants that at all).

到目前为止,我将文件列表公开为公共字符串属性,并且使用者可以将代码添加到init并将其存储在应用程序配置中。如果可能的话,我想进一步简化这一点,以便消费者只需提供设置变量,他希望存储设置(如果他想要的话)。

3 个解决方案

#1


I don't think your control should be adding anything to the config file, unless it's been asked to do so.

我不认为您的控件应该在配置文件中添加任何内容,除非有人要求它这样做。

Still, if the user "opts in" to this, then I think you're going to have to add some designer support to your control. Either through the ToolBoxItem, or through the designer being notified on a new control being added, the designer will then have to create and/or update the configuration information it needs.

尽管如此,如果用户“选择”此操作,那么我认为您将不得不为您的控件添加一些设计器支持。无论是通过ToolBoxItem,还是通过设计器在添加新控件时得到通知,设计人员都必须创建和/或更新所需的配置信息。


Thanks for the clarification. You may want to look at IPersistControlSettings interface, especially what it has to say on the subject of ApplicationSettingsBase. That page should lead you to information on strongly-typed Settings in .NET 2.0. It allows default settings to be baked into a control or other library code, in such a way that if the settings change, the changes persist to the application's config file, either per-user or application-wide.

谢谢你的澄清。您可能希望查看IPersistControlSettings接口,尤其是它对ApplicationSettingsBase主题的看法。该页面应引导您获取有关.NET 2.0中强类型设置的信息。它允许将默认设置烘焙到控件或其他库代码中,以便在设置更改时,更改将持久保存到应用程序的配置文件(每个用户或应用程序范围)。

#2


I think it depends on how much value you will get from the consumer of the control having a usable experience. Are you selling this control? Is it for internal use? The effort required to create the design time experience would probably be prohibitive if this is for internal use.

我认为这取决于您将从具有可用体验的控件的消费者获得多少价值。你在卖这个控件吗?它是供内部使用的吗?如果用于内部使用,创建设计时体验所需的努力可能会令人望而却步。

#3


In your control constructor you can check (LicenseManager.UsageMode == LicenseUsageMode.Designtime) and then check the config entries etc but I think this would be a bad idea

在您的控件构造函数中,您可以检查(LicenseManager.UsageMode == LicenseUsageMode.Designtime),然后检查配置条目等,但我认为这将是一个坏主意

Your control could support config entries if they exist, and have some default behaviour if they did not. That would seem more acceptable to me as a developer

您的控件可以支持配置条目(如果存在),并且如果它们不存在则具有一些默认行为。作为开发人员,这似乎更容易被我接受

You should use a custom config section either way

您应该使用自定义配置部分

You can provide a custom config section handler as part of the control so all that need to be done is wire this section up in the config file if the use wants it

你可以提供一个自定义配置节处理程序作为控件的一部分,所以所有需要做的就是如果使用需要它将这部分连接到配置文件中

#1


I don't think your control should be adding anything to the config file, unless it's been asked to do so.

我不认为您的控件应该在配置文件中添加任何内容,除非有人要求它这样做。

Still, if the user "opts in" to this, then I think you're going to have to add some designer support to your control. Either through the ToolBoxItem, or through the designer being notified on a new control being added, the designer will then have to create and/or update the configuration information it needs.

尽管如此,如果用户“选择”此操作,那么我认为您将不得不为您的控件添加一些设计器支持。无论是通过ToolBoxItem,还是通过设计器在添加新控件时得到通知,设计人员都必须创建和/或更新所需的配置信息。


Thanks for the clarification. You may want to look at IPersistControlSettings interface, especially what it has to say on the subject of ApplicationSettingsBase. That page should lead you to information on strongly-typed Settings in .NET 2.0. It allows default settings to be baked into a control or other library code, in such a way that if the settings change, the changes persist to the application's config file, either per-user or application-wide.

谢谢你的澄清。您可能希望查看IPersistControlSettings接口,尤其是它对ApplicationSettingsBase主题的看法。该页面应引导您获取有关.NET 2.0中强类型设置的信息。它允许将默认设置烘焙到控件或其他库代码中,以便在设置更改时,更改将持久保存到应用程序的配置文件(每个用户或应用程序范围)。

#2


I think it depends on how much value you will get from the consumer of the control having a usable experience. Are you selling this control? Is it for internal use? The effort required to create the design time experience would probably be prohibitive if this is for internal use.

我认为这取决于您将从具有可用体验的控件的消费者获得多少价值。你在卖这个控件吗?它是供内部使用的吗?如果用于内部使用,创建设计时体验所需的努力可能会令人望而却步。

#3


In your control constructor you can check (LicenseManager.UsageMode == LicenseUsageMode.Designtime) and then check the config entries etc but I think this would be a bad idea

在您的控件构造函数中,您可以检查(LicenseManager.UsageMode == LicenseUsageMode.Designtime),然后检查配置条目等,但我认为这将是一个坏主意

Your control could support config entries if they exist, and have some default behaviour if they did not. That would seem more acceptable to me as a developer

您的控件可以支持配置条目(如果存在),并且如果它们不存在则具有一些默认行为。作为开发人员,这似乎更容易被我接受

You should use a custom config section either way

您应该使用自定义配置部分

You can provide a custom config section handler as part of the control so all that need to be done is wire this section up in the config file if the use wants it

你可以提供一个自定义配置节处理程序作为控件的一部分,所以所有需要做的就是如果使用需要它将这部分连接到配置文件中