简体   繁体   English

多台windows之间切换

[英]Switch between multiple windows

We have a software system that should support multiple variants.我们有一个应该支持多种变体的软件系统。 Each variant should contain a customized version of one or more UI components (Windows Forms in this case).每个变体都应包含一个或多个 UI 组件的自定义版本(在本例中为 Windows Forms)。

A prototype has been created in VS2017 and has the following solution;在 VS2017 中创建了一个原型,并有以下解决方案; ProductFoo (Solution) ProductFoo(解决方案)

  • MainApplication (Windows Application)主应用程序(Windows 应用程序)
    • MainApplicationForm1.cs (Windows Forms) MainApplicationForm1.cs(Windows 窗体)
  • XY1 (Class library) XY1(类库)
    • Form1.cs ((Windows Forms) Form1.cs ((Windows 窗体)
  • XY2 (Class library) XY2(类库)
    • Form1.cs ((Windows Forms) Form1.cs ((Windows 窗体)

In this simple prototype, the MainApplicationForm1 forms contain one button that when clicked should either show Form1.cs in XY1 og XY2 library depending on which variant is selected.在这个简单的原型中,MainApplicationForm1 forms 包含一个按钮,单击该按钮应在 XY1 og XY2 库中显示Form1.cs ,具体取决于选择的变体。

To solve this we have updated Solution Manager with following solution configurations;为了解决这个问题,我们使用以下解决方案配置更新了解决方案管理器;

  • XY1_Debug XY1_调试
  • XY1_Release XY1_Release
  • XY2_Debug XY2_调试
  • XY2_Release XY2_Release

Then we added conditional compilation symbols for MainApplication.然后我们为 MainApplication 添加了条件编译符号。

  • The solution configurations XY1_Debug and XY1_Release use the conditional symbol XY1解决方案配置 XY1_Debug 和 XY1_Release 使用条件符号 XY1
  • The solution configurations XY2_Debug and XY2_Release use the conditional symbol XY2解决方案配置 XY2_Debug 和 XY2_Release 使用条件符号 XY2

Then we added reference from MainApplication to both XY1 and XY2 projects.然后我们将 MainApplication 的引用添加到 XY1 和 XY2 项目。

Lastly, we added the following code in MainApplicationForm1.cs最后,我们在 MainApplicationForm1.cs 中添加了以下代码

public partial class MainAppForm1 : Form
{
    public MainAppForm1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
    #if XY1
        XY1.Form1 form = new XY1.Form1();
        form.ShowDialog();
    #elif XY2
        XY2.Form1 f1 = new XY2.Form1();
        f1.ShowDialog();
    #else
        #error The MainApplication is missing Form1
    #endif
    }
}

This solution works but I have reservations with using preprocessor directives.该解决方案有效,但我对使用预处理器指令有所保留。 The code looks messy and can quickly become difficult to maintain.代码看起来很乱,很快就会变得难以维护。 What are the best practices for this kind of scenario?这种场景的最佳实践是什么?

Appreciate any input.感谢任何输入。

Your question is quite broad and referes to the base structure of the project you want to have.您的问题非常广泛,涉及您想要拥有的项目的基本结构。

The way you choose is close to the Feature toggling , just done based on the build configuration.您选择的方式接近Feature toggling ,只是根据构建配置完成。 Ususaly it sould be something like: Ususaly 它应该是这样的:

if(features.IsOn("XY1-feature")){
    XY1.Form1 form = new XY1.Form1();
    form.ShowDialog();
}

Classical way can give you more flexibility.经典的方式可以给你更多的灵活性。 Eg moving feature toggless to the config would give you a possibility to dynamically toggle different features for specific deployment, but, as impact, it would encrease the complexity and would require more testing例如,将功能切换到配置将使您能够为特定部署动态切换不同的功能,但作为影响,它会增加复杂性并需要更多测试

I would suggest you to take a deeper look into Dependency injection and Strategy pattern我建议您更深入地了解依赖注入策略模式

As an alternative to the Feature toggling you can use branching .作为功能切换的替代方法,您可以使用分支 Create a specific branch for the specific project/client.为特定项目/客户创建特定分支。 That could bring you problems with merging, but would keep your cleaner for a specific implementation.这可能会给您带来合并问题,但会让您更清洁特定的实现。 It would fit best to the project with lots of minor differences from project to project它最适合项目,项目之间有很多细微差别

I suggest using two radio buttons to solve this problem.我建议使用两个单选按钮来解决这个问题。 This is a very easy way.这是一个非常简单的方法。

Select radioButton1, pop up XY1.Form Select radioButton1,弹出XY1.Form

Select radioButton2, pop up XY2.Form Select radioButton2,弹出XY2.Form

MainApplicationForm1.cs: MainApplicationForm1.cs:

private void radioButton1_CheckedChanged(object sender, EventArgs e)

    {
        if (radioButton1.Checked) 
        {
            XY1.Form1 form = new XY1.Form1();
            form.Show();                
        }
        
    }
    private void radioButton2_CheckedChanged(object sender, EventArgs e)
    {
        if (radioButton2.Checked)
        {
            XY2.Form1 f1 = new XY2.Form1();
            f1.Show();               
        }
    }

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

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