如何简化主窗体和子窗体之间传递的设置值的代码

发布于 2024-12-18 09:23:14 字数 1091 浏览 6 评论 0原文

我有一个类存储我的应用程序的设置。它是在应用程序时实例化的。应用程序运行并保存。关闭。

public class Settings
{
    public bool showPrivacyPageOnBlogs;
    public bool showTermsPageOnBlogs;
    public bool showDisclosurePageOnBlogs;
}

还有一个弹出窗口,显示复选框以使用弹出窗口的公共属性设置这些值。

处理弹出窗口的代码如下:

// Horrible code ahead
private void pagesSettingsToolStripMenuItem1_Click(object sender, EventArgs e)
{
    pagesSettingsForm.showPrivacyPageOnBlogs = settings.showPrivacyPageOnBlogs;
    pagesSettingsForm.showTermsPageOnBlogs = settings.showTermsPageOnBlogs;
    pagesSettingsForm.showDisclosurePageOnBlogs = settings.showDisclosurePageOnBlogs;
    if (pagesSettingsForm.ShowDialog() == DialogResult.OK)
    {
        settings.showPrivacyPageOnBlogs = pagesSettingsForm.showPrivacyPageOnBlogs;
        settings.showTermsPageOnBlogs = pagesSettingsForm.showTermsPageOnBlogs;
        settings.showDisclosurePageOnBlogs = pagesSettingsForm.showDisclosurePageOnBlogs;
    }
    pagesSettingsForm.Dispose();
}

在我的应用程序中。还有更多参数以这种方式处理,所以我想知道是否有某种方法可以简化此代码,以便枚举设置的名称并允许将来添加其他参数。

I have a class that stores settings for my app. It is instantiated when the app. runs and saved when the app. closes.

public class Settings
{
    public bool showPrivacyPageOnBlogs;
    public bool showTermsPageOnBlogs;
    public bool showDisclosurePageOnBlogs;
}

And there is a popup window that displays check boxes to set these values using public properties of the popup window.

The code to handle the popup window is like:

// Horrible code ahead
private void pagesSettingsToolStripMenuItem1_Click(object sender, EventArgs e)
{
    pagesSettingsForm.showPrivacyPageOnBlogs = settings.showPrivacyPageOnBlogs;
    pagesSettingsForm.showTermsPageOnBlogs = settings.showTermsPageOnBlogs;
    pagesSettingsForm.showDisclosurePageOnBlogs = settings.showDisclosurePageOnBlogs;
    if (pagesSettingsForm.ShowDialog() == DialogResult.OK)
    {
        settings.showPrivacyPageOnBlogs = pagesSettingsForm.showPrivacyPageOnBlogs;
        settings.showTermsPageOnBlogs = pagesSettingsForm.showTermsPageOnBlogs;
        settings.showDisclosurePageOnBlogs = pagesSettingsForm.showDisclosurePageOnBlogs;
    }
    pagesSettingsForm.Dispose();
}

In my app. there are several more parameters being handled in this way so I would like to know if there is some way to simplify this code to maybe enumerate the names of the settings and allow for future addition of additional parameters.

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

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

发布评论

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

评论(3

怀里藏娇 2024-12-25 09:23:14

只需让表单公开一个带有 getter 和 setter 的 Settings 类型的属性即可。这使得您发布的代码片段变得简单,在将成员添加到“设置”时无需进行任何更改。现在工作转移到表单实现上。 PropertyGrid 是一个通用的对象编辑器,很难猜测它在您的情况下是否足够可用。

Simply have the form expose a property of type Settings with a getter and a setter. That makes the snippet you posted simple without any changes needed when you add members to Settings. The effort now moves to the form implementation. PropertyGrid is a generic object editor, whether it is usable enough in your case is hard to guess.

百善笑为先 2024-12-25 09:23:14

虽然我还没有尝试过,但我坚信 Automapper 可以处理这个问题。我认为这可能会让你的代码变成这样:

// Horrible code ahead
private void pagesSettingsToolStripMenuItem1_Click(object sender, EventArgs e)
{
    Mapper.Map(settings, pagesSettingsForm);
    if (pagesSettingsForm.ShowDialog() == DialogResult.OK)
        Mapper.Map(pagesSettingsForm, settings);
    pagesSettingsForm.Dispose();
}    

PS:我知道你说代码很糟糕,但我不能不提到你正在处理一个在其他代码中实例化的表单 - 这是错误的,国际海事组织。

Though I haven't tried that, but I strongly believe that Automapper can handle this. I think it may make your code to be like this:

// Horrible code ahead
private void pagesSettingsToolStripMenuItem1_Click(object sender, EventArgs e)
{
    Mapper.Map(settings, pagesSettingsForm);
    if (pagesSettingsForm.ShowDialog() == DialogResult.OK)
        Mapper.Map(pagesSettingsForm, settings);
    pagesSettingsForm.Dispose();
}    

P.S.: I know that you said that code is horrible, but I can't not to mention that you're disposing a form which is instantiated in some other code - which is wrong, IMO.

倾`听者〃 2024-12-25 09:23:14

使用字典

Dictionary<String,dynamic> 

会很好,因为它会推迟输入,

因此您可以在设置类中为应用程序所需的所有设置添加/设置设置。
您的表单有它自己的实例,但仅包含它需要的实例。
然后,只需扫描 form.settings 中传递的设置中的相同命名版本并覆盖它们即可。

Use a dictionary

Dictionary<String,dynamic> 

would be nice as it would defer typing

So you add/set settings in your settings class for all the setting you need app wise.
You form has it's own instance of it, but only with the ones it needs.
Then it would be just matter of scanning down form.settings for same named versions in the passed settingsw and overwriting them.

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