简体   繁体   中英

Inherit from "Application" in wpf application

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?

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.

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