简体   繁体   English

如何在C#中使用Factory模式来选择我想要的GUI:WPF或控制台?

[英]How to make Factory pattern in c# to choose which GUI i want : WPF or Console?

I make a calculator program. 我做一个计算器程序。 I made four projects. 我做了四个项目。

  1. class library which has the logic of the calculator. 具有计算器逻辑的类库。
  2. calculator gui in wpf application. WPF应用程序中的计算器GUI。
  3. calculator gui in console application. 控制台应用程序中的计算器gui。
  4. Factory class library which is the "startup project". 工厂类库,即“启动项目”。

The GUI applications (WPF or Console) works great! GUI应用程序(WPF或控制台)效果很好! they have the "logic class library" in their dependencies. 他们在其依赖项中具有“逻辑类库”。

Now i want from my client to choose which GUI he wants, so i wrote for him Factory class library and make it "startup project". 现在,我想从我的客户那里选择他想要的GUI,因此我为他编写了Factory类库并将其设置为“启动项目”。

But it doesn't work because the error message "class library cannot be started directly". 但这是行不通的,因为错误消息“类库无法直接启动”。

What to do? 该怎么办?

thanks in ahead. 谢谢你。

Class library cannot be startup project. 类库不能是启动项目。 It must be exe, not dll. 它必须是exe,而不是dll。

What I would do is: 我要做的是:

  • create something like start.bat which asks a user which gui version he wants. 创建类似start.bat之类的内容,询问用户他想要哪个gui版本。 Then batch would run wpf.exe or console.exe 然后批处理将运行wpf.exe或console.exe

or 要么

  • create simple console application that will do the same thing. 创建将执行相同操作的简单控制台应用程序。 After starting exec it will shutdown. 启动exec后,它将关闭。

Console app or batch may also read some registry entry and based on that call apropriate exec. 控制台应用程序或批处理程序可能还会读取某些注册表项,并基于该调用适当的exec。

The easiest way is to create another application which it's only task is to start either the GUI version or the CLI version. 最简单的方法是创建另一个应用程序,它的唯一任务是启动GUI版本或CLI版本。

You could do this in a dedicated console application , but then you'll see the console box popup for a small second. 您可以在专用的console application执行此操作,但是稍后您会看到console box弹出窗口。

As alternative you could use a WinForms application and use the folowing lines of code: 或者,您可以使用WinForms应用程序并使用以下代码行:

using System.Diagnostics;

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string [] args)
    {
        // disable these lines:
        //Application.EnableVisualStyles();
        //Application.SetCompatibleTextRenderingDefault(false);
        //Application.Run(new Form1());

        //read settings file or parse arguments

        //start the GUI or CLI process
        Process process = new Process();
        process.StartInfo.FileName = "some.exe";
        process.Start();
        //optional
        process.WaitForExit();
    }
}

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

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