简体   繁体   中英

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.
  3. calculator gui in console application.
  4. Factory class library which is the "startup project".

The GUI applications (WPF or Console) works great! 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".

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.

What I would do is:

  • create something like start.bat which asks a user which gui version he wants. Then batch would run wpf.exe or console.exe

or

  • create simple console application that will do the same thing. After starting exec it will shutdown.

Console app or batch may also read some registry entry and based on that call apropriate 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.

You could do this in a dedicated console application , but then you'll see the console box popup for a small second.

As alternative you could use a WinForms application and use the folowing lines of code:

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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