.net 替换 WPF 入口点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6156550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replacing the WPF entry point
提问by Qwertie
WPF defines its own Main()method. How should I go about replacing it with my own Mainmethod that (normally) opens the WPF MainWindow(e.g. to add a non-WPF scripting mode via command-line arguments)?
WPF 定义了自己的Main()方法。我应该如何用我自己的Main方法替换它(通常)打开 WPF MainWindow(例如通过命令行参数添加非 WPF 脚本模式)?
采纳答案by Joel B Fant
Some examples depict changing App.xaml's Build Action from ApplicationDefinitionto Pageand writing your own Main()that instantiates the Appclass and calls its Run()method, but this can produce some unwanted consequences in the resolution of application-wide resources in App.xaml.
一些示例描述了将 App.xaml 的构建操作从 更改ApplicationDefinition为Page并编写自己的Main()实例化App类并调用其Run()方法,但这可能会在 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:
相反,我建议Main()在自己的类中创建自己的类,并在项目属性中将启动对象设置为该类:
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();
}
}
}
I do this to take advantage of some AppDomainevents that must be subscribed to before anything else happens (such as AssemblyResolve). The unwanted consequences of setting App.xaml to Pagethat I experienced included my UserControlViews (M-V-VM) not resolving resources held in App.xaml during design-time.
我这样做是为了利用一些AppDomain必须在其他任何事情发生之前订阅的事件(例如AssemblyResolve)。将 App.xaml 设置为Page我遇到的不良后果包括我的UserControl视图 (MV-VM) 在设计时无法解析 App.xaml 中保存的资源。
回答by user7116
Typically I edit App.xamlto add this support:
通常我会编辑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">
The relevant part being I changed from StartupUrito Startupwith an event handler in App.xaml.cs. Here is an example:
相关部分正在从我改StartupUri到Startup与事件处理程序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();
}
}
回答by Rayan Elmakki
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:
问题是你的程序有两个静态 Main() 方法,这会导致编译器之间抱怨;要解决此问题,请尝试以下操作之一:
- 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”.
- 告诉编译器您的静态 Main() 方法应该是执行入口点——将您项目的“启动对象”设置设置为包含您的静态 Main() 方法的类(右键单击解决方案资源管理器中的项目,选择“属性, ”,然后在“应用程序”选项卡下查找“启动对象”设置)。
- 关闭 App.g.cs 的静态 Main() 方法的自动生成——在解决方案资源管理器中,右键单击 App.xaml,选择“属性”,然后将“构建操作”从“ApplicationDefinition”更改为“Page”。
回答by Vlad Rudenko
Create new class with your custom static Main method. At the end of this method just call original App.Main() generated by WPF:
使用自定义静态 Main 方法创建新类。在这个方法的最后只调用 WPF 生成的原始 App.Main() :
public class Program
{
[STAThread]
public static void Main(string[] args)
{
// Your initialization code
App.Main();
}
}
Then set your project's “Startup object” setting to the class containing your static Main().
然后将项目的“启动对象”设置为包含静态 Main() 的类。
回答by ecreif
Using a custom Main() you might run into problems because StartupUri is not set.
使用自定义 Main() 您可能会遇到问题,因为 StartupUri 未设置。
You can use this to set it without headaches in your App class (Don't forget to remove StartupUri from App.xaml and set its Build Action to Page):
您可以使用它在您的 App 类中轻松设置它(不要忘记从 App.xaml 中删除 StartupUri 并将其构建操作设置为 Page):
[STAThread]
static void Main()
{
App app = new App();
app.InitializeComponent();
app.Run();
}
protected void OnStartup(object sender, StartupEventArgs e)
{
var toUri = new UriTypeConverter();
StartupUri = (Uri)toUri.ConvertFrom("MainWindow.xaml");
...
}

