替换WPF入口点

Qwe*_*tie 54 .net wpf entry-point

WPF定义了自己的Main()方法.我应该如何用我自己的Main方法替换它(通常)打开WPF MainWindow(例如通过命令行参数添加非WPF脚本模式)?

Joe*_*ant 53

一些示例描述了将App.xaml的构建操作从更改ApplicationDefinitionPage编写自己的Main()实例化App类并调用其Run()方法,但这会在App.xaml中解决应用程序范围的资源时产生一些不必要的后果.

相反,我建议Main()在自己的类中创建自己的类,并在项目属性中将Startup Object设置为该类:

public class EntryPoint {
    [STAThread]
    public static void Main(string[] args) {
        if (args != null && args.Length > 0) {
            // ...
        } else {
            var app = new App();
            app.InitializeComponent();
            app.Run();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我这样做是为了利用AppDomain在发生任何其他事情之前必须订阅的一些事件(例如AssemblyResolve).将App.xaml设置为Page我所经历的不良后果包括我的UserControl视图(MV-VM)在设计时没有解析App.xaml中保存的资源.

  • 生成的`Main()`只是实例化`App`并调用`Run()`.`Startup`事件在`System.Windows.Application`的构造函数中触发.`Run()`附加一个`Dispatcher`并开始消息泵.应该在`Apps`的构造函数中调用`InitializeComponent()`.不是吗? (3认同)
  • 我向`App` 添加了一个构造函数并在那里调用`InitializeComponent()` 以避免`App.Main()`。为简洁起见,我将其余部分过度简化。`Startup` 由 `Application.OnStartup()` 触发,并且只有派生的 `App` 类的构造函数可以在它被触发之前订阅 `Startup`。这是有效的,因为`Application` 的构造函数异步调用一个调用`OnStartup()` 的方法,所以它实际上是在基类和派生构造函数完成之后运行的。 (2认同)

use*_*116 20

通常我编辑App.xaml添加此支持:

<Application x:Class="SomeNamespace.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         Startup="Application_Startup">
Run Code Online (Sandbox Code Playgroud)

相关的部分是我从改变StartupUriStartup与事件处理程序App.xaml.cs.这是一个例子:

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        int verbose = 0;
        var optionSet = new OptionSet
        {
            { "v|verbose", "verbose output, repeat for more verbosity.",   
                    arg => verbose++ }
        };

        var extra = optionSet.Parse(e.Args);
        var mainWindow = new MainWindow(verbose);
        mainWindow.Show();
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 17

guys The problem is that your program has two static Main() methods, that will cause the compiler to complain between; To resolve this, try one of the following:

  • Tell the compiler that your static Main() method should be the execution entry point—Set your project’s “Startup object” setting to the class containing your static Main() method (right-click on the project in Solution Explorer, choose “Properties,” then look for the “Startup object” setting under the “Application” tab).
  • Turn off auto-generation of App.g.cs’s static Main() method—In Solution Explorer, right click on App.xaml, choose “Properties,” then change the “Build Action” from “ApplicationDefinition” to “Page”.

  • 谢谢; 第二个要点是至关重要的 - 巧妙地放在那里! (3认同)

Vla*_*nko 5

使用您的自定义静态 Main 方法创建新类。在此方法的最后,只需调用 WPF 生成的原始 App.Main() 即可:

\n\n
public class Program\n{\n    [STAThread]\n    public static void Main(string[] args)\n    {\n        // Your initialization code\n        App.Main();\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后将您的project\xe2\x80\x99s \xe2\x80\x9cStartup object\xe2\x80\x9d 设置设置为包含静态Main() 的类。

\n