改变主要在WPF项目项目、WPF

2023-09-04 05:12:29 作者:【天堂の纵横四海】

总是让我生气了C#想做的启动为您服务。所以,现在我想使自己的主要方法。它不工作:

Always annoyed me how C# wants to do the startup for you. So now I am trying to make my own main method. It's not working:

我已经提供了这个主要方法:

I have provided this main method:

[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static void Main()
{
    Model model= new Model();
    Controller controller = new Controller(model);
    MainWindow window = new MainWindow(controller, model);
}

此方法运行,但我看不到任何东西的视觉享受。 我想我错过下面的正常主code的内容:

This method is run, but I cant see anything visual. I think I miss something from the following normal main code:

Application.App app = new Application.App();
app.InitializeComponent();
app.Run();

我试图重写OnStartUp witht相同的code,但抛出异常。 如果您有其他解决方案,我很愿意听到的。我只是不明白为什么我的主窗口,必须先创建。

I have tried overriding OnStartUp witht the same code, but that throws an exception. If you have other solutions I am willing to hear. I just can't see why my MainWindow has to be created first.

推荐答案

你是对的:你需要实例化一个应用程序并调用运行就可以了。 (你会在主要做到这一点)。为了使它显示你的窗口在运行时,有三种选择:

You're right: you need to instantiate an Application and call Run on it. (You'd do this in Main.) To make it show your window when it runs, there are three options:

使用运行(窗口)的过载,例如主窗口W = ...;新的MyApp()运行(W); 设置中的StartupUri例如 myApp.StartupUri =新的URI(...); myApp.Run(); 请显示在启动事件或OnStartup覆盖如 myApp.Startup + =(...)=>新的主窗口()显示(); Use the Run(Window) overload, e.g. MainWindow w = ...; new MyApp().Run(w); Set the StartupUri e.g. myApp.StartupUri = new Uri(...); myApp.Run(); Do the Show in the Startup event or OnStartup override e.g. myApp.Startup += (...) => new MainWindow().Show();

手动启动code例子在 Application.Run()和 Application.Run(窗口)项 - 这应该让你开始!该运行()超载还讨论的为什么的Application.Run是需要的,它做什么,例如启动调度循环。

Examples of manual startup code are shown in MSDN under the Application.Run() and Application.Run(Window) entries - these should get you started! The Run() overload also discusses why Application.Run is needed and what it does e.g. starting the dispatcher loop.

 
精彩推荐
图片推荐