-2

I'm currently building a set of application in WPF, they will use Prism as framework.

They will have a lot in common, and the only thing that will changes between 2 of my app is what will be loaded in the Prism region.

So I wanted to put some things in common.

One thing that I didn't succeed is to make one common "Application" implementation with all the common part:

The goal was to have:

Application <-- BaseApp(abstract) <-- MySpecificAppplication(code behind only)

The goal is to have every resources declarations in BaseApplication, the OnStartup implementation in common, and only the specific part(in my case, which Bootstrapper I want to run) in the end class.

Here are some code:

public abstract partial class BaseApp : Application //This class has also some XAML for resources loading, but pretty basic.
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        BaseBootstrapper clientBootstrapper = GetBootstrapper();
        //Some initialization stuff
        clientBootstrapper.Run();//This will cause Prism to load the correct first View(shell)
    }
    protected abstract BaseBootstrapper GetBootstrapper();
}

And the specific implementation

public  class MySpecificApp : BaseApp
{
    protected override BaseBootstrapper GetBootstrapper(){
        return //something ;
    }
}

But I build, I get an error:

Program does not contain a static 'Main' method suitable for an entry point

Is this supported?

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
J4N
  • 19,480
  • 39
  • 187
  • 340

1 Answers1

1

When you create a new WPF Application in Visual Studio using the default template, you get an App class that do inherit from System.Windows.Application.

The compiler adds a Main() method to this one for you, and in this method an instance of the App class is created and the InitializeComponent() method is called to parse the XAML before the Run() method is called to start the application.

You can add define the Main() method yourself if you want to:

[System.STAThreadAttribute()]
public static void Main()
{
    App app = new App();
    app.InitializeComponent();
    app.Run();
}

But obviously you cannot create an instance of an abstract class. So if BaseApp contains XAML, it shouldn't be defined as an abstract class.

The alternative would be move the XAML and the Main() method to your MySpecificApp class and create an instance of this one in the Main() method.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • I see, but then I loose the "I don't want to import my set of styles 10 times", any workaround? – J4N Feb 22 '18 at 20:22
  • Put the styles in a resource dictionary that you merge into the app or window? – mm8 Feb 23 '18 at 10:36