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

发布于 2024-07-14 12:16:07 字数 385 浏览 8 评论 0原文

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

这是可取的吗? 做到这一点的“好”方法是什么?

[编辑]
该控件为文件菜单提供了默认实现,使用者只需在设计器中连接菜单/工具栏项,并实现一些基本事件。 其中一部分是最近的文件列表,默认情况下应该记住它。

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

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

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屌丝范 2024-07-21 12:16:07

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

不过,如果用户“选择加入”这一点,那么我认为您将必须为您的控件添加一些设计器支持。 无论是通过 ToolBoxItem,还是通过通知设计者添加新控件,设计者都必须创建和/或更新其所需的配置信息。


感谢您的澄清。 您可能需要查看 IPersistControlSettings 界面,尤其是关于 ApplicationSettingsBase 的主题有什么要说的。 该页面应该会引导您找到有关 .NET 2.0 中强类型设置的信息。 它允许将默认设置烘焙到控件或其他库代码中,这样,如果设置发生更改,这些更改将保留到应用程序的配置文件中,无论是针对每个用户还是在应用程序范围内。

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.


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.

吻泪 2024-07-21 12:16:07

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

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.

皓月长歌 2024-07-21 12:16:07

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

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

无论哪种方式,您都应该使用自定义配置部分。

您可以提供自定义配置部分处理程序作为控件的一部分,因此需要做的就是将此部分连接到配置文件中,如果使用想要它

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文