更换WPF切入点 [英] Replacing the WPF entry point

查看:238
本文介绍了更换WPF切入点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF定义了自己的主要()方法。我应该如何去用我自己的方法(正常)打开WPF替换它主窗口(如添加通过命令行参数的非WPF脚本模式)?

WPF defines its own Main() method. How should I go about replacing it with my own Main method that (normally) opens the WPF MainWindow (e.g. to add a non-WPF scripting mode via command-line arguments)?

推荐答案

一些示例描述的改变从 ApplicationDefinition App.xaml中的生成操作,写你自己的主要()实例化应用类,并调用其运行()的方法,但可以在App.xaml中产生的应用范围的资源解决一些不必要的后果。

Some examples depict changing App.xaml's Build Action from ApplicationDefinition to Page and writing your own Main() that instantiates the App class and calls its Run() method, but this can produce some unwanted consequences in the resolution of application-wide resources in App.xaml.

相反,我建议制作自己的主要()在其自己的类并设置启动对象到类在项目属性:

Instead, I suggest making your own Main() in its own class and setting the Startup Object to that class in the project properties:

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

我这样做是为了利用某些的AppDomain 事件,必须在任何其他情况(如 AssemblyResolve )。设置App.xaml中来的不良后果,我经历了包括我的用户控件视图(MV-VM)不是解决在设计时在App.xaml中保留的资源。

I do this to take advantage of some AppDomain events that must be subscribed to before anything else happens (such as AssemblyResolve). The unwanted consequences of setting App.xaml to Page that I experienced included my UserControl Views (M-V-VM) not resolving resources held in App.xaml during design-time.

这篇关于更换WPF切入点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆