The Main
method is the entry point of a C# console application. Thus, for example, if I have to start some threads or services, I will do it within the Main
method.
主方法是c#控制台应用程序的入口点。因此,例如,如果我必须启动一些线程或服务,我将在Main方法中执行。
I do not see the Main
method inside a WPF project, so what is the entry point of a WPF application? If I have to start some threads or services, where should write the code for starting them?
我在WPF项目中看不到主方法,那么WPF应用程序的入口点是什么?如果我必须启动一些线程或服务,应该在哪里编写启动它们的代码?
UPDATE: this answer summarizes the available solutions, but what are the pros and cons of each solution?
更新:这个答案总结了可用的解决方案,但是每个解决方案的优缺点是什么?
4 个解决方案
#1
19
Your main entry point is an override of OnStartup in the code-behind of App.Xaml :
您的主要入口点是App.Xaml代码后的OnStartup的重写:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// here you take control
}
}
Other points of interest might be Application.OnActivate() and the Loaded and Initialized events of your MainWindow.
其他值得关注的点可能是Application.OnActivate()和主窗口的已加载和初始化事件。
If I have to start some threads or services, where should write the code for starting them?
如果我必须启动一些线程或服务,应该在哪里编写启动它们的代码?
Depends on what those threads/services need and want.
取决于这些线程/服务需要什么和需要什么。
#2
29
For a WPF standalone application that is generated in Visual Studio using the New Project wizard, the entry point for the application is the Main function, defined in App.g.cs (generated code). In the default project, this is the public static void App.Main method.
对于在Visual Studio中使用New Project向导生成的WPF独立应用程序,应用程序的入口点是在App.g中定义的主函数。cs(生成的代码)。在默认项目中,这是公共静态void App.Main方法。
In general, a .NET application will use as its entry point (first function called) any method named Main that has public/static access modifiers–no matter what class Main is located in.
一般来说,. net应用程序将使用它的入口点(第一个函数调用)任何具有公共/静态访问修饰的方法Main—无论类Main位于何处。
If your application has more than one class with a public static Main method, you’ll need to specify the entry point in the project properties dialog. In the Startup object dropdown, select the class that contains the Main method that should be called on startup.
如果您的应用程序具有一个公共静态Main方法的多个类,那么您需要在项目属性对话框中指定入口点。在Startup对象下拉菜单中,选择包含启动时应该调用的主方法的类。
#3
7
The Main for a WPF application is autogenerated and can be found in one of the .cs files that backs your App.xaml file. You can expand App.xaml -> App.xaml.cs -> App -> Main() in the solution explorer, which will get you to the App.g.i.cs source file, which contains your Main() function.
WPF应用程序的主要部分是自动生成的,可以在支持App.xaml文件的.cs文件中找到。您可以展开App.xaml -> App.xaml。cs -> App -> Main()在解决方案资源管理器中,它会将您带到App. gi。cs源文件,其中包含主()函数。
This file is auto-generated, so rather than editing the Main there, I would recommend creating a new .cs file in your project that contains the Main() function. You then have to change the properties of your project to specify the correct startup object. This is done on the Application tab in your project properties. Set it to the class that contains your custom Main function.
这个文件是自动生成的,所以我建议在项目中创建一个包含Main()函数的.cs文件,而不是编辑主文件。然后必须更改项目的属性以指定正确的启动对象。这是在项目属性中的Application选项卡上完成的。将它设置为包含自定义主函数的类。
You probably want to copy the contains of the autogenerated Main into your new one, since you want your application to behave normally (show the main window, etc.).
您可能希望将自动生成的Main的包含内容复制到新的Main中,因为您希望应用程序正常运行(显示主窗口等)。
#4
2
Entry point is App.xaml.cs typically.
App.xaml入口点。cs一般。
You want to avoid putting code there ideally. Instead try instantiating them in view models for MVVM. It's typically a tricky place to find stuff - as your question is testament to.
您希望避免在理想情况下放置代码。而是尝试在MVVM的视图模型中实例化它们。这通常是一个很难找到东西的地方——正如你的问题所证明的那样。
Another alternative, load them in a helper class and then instantiate that in the app.xaml file.
另一种方法是,在helper类中加载它们,然后在app.xaml文件中实例化它们。
#1
19
Your main entry point is an override of OnStartup in the code-behind of App.Xaml :
您的主要入口点是App.Xaml代码后的OnStartup的重写:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// here you take control
}
}
Other points of interest might be Application.OnActivate() and the Loaded and Initialized events of your MainWindow.
其他值得关注的点可能是Application.OnActivate()和主窗口的已加载和初始化事件。
If I have to start some threads or services, where should write the code for starting them?
如果我必须启动一些线程或服务,应该在哪里编写启动它们的代码?
Depends on what those threads/services need and want.
取决于这些线程/服务需要什么和需要什么。
#2
29
For a WPF standalone application that is generated in Visual Studio using the New Project wizard, the entry point for the application is the Main function, defined in App.g.cs (generated code). In the default project, this is the public static void App.Main method.
对于在Visual Studio中使用New Project向导生成的WPF独立应用程序,应用程序的入口点是在App.g中定义的主函数。cs(生成的代码)。在默认项目中,这是公共静态void App.Main方法。
In general, a .NET application will use as its entry point (first function called) any method named Main that has public/static access modifiers–no matter what class Main is located in.
一般来说,. net应用程序将使用它的入口点(第一个函数调用)任何具有公共/静态访问修饰的方法Main—无论类Main位于何处。
If your application has more than one class with a public static Main method, you’ll need to specify the entry point in the project properties dialog. In the Startup object dropdown, select the class that contains the Main method that should be called on startup.
如果您的应用程序具有一个公共静态Main方法的多个类,那么您需要在项目属性对话框中指定入口点。在Startup对象下拉菜单中,选择包含启动时应该调用的主方法的类。
#3
7
The Main for a WPF application is autogenerated and can be found in one of the .cs files that backs your App.xaml file. You can expand App.xaml -> App.xaml.cs -> App -> Main() in the solution explorer, which will get you to the App.g.i.cs source file, which contains your Main() function.
WPF应用程序的主要部分是自动生成的,可以在支持App.xaml文件的.cs文件中找到。您可以展开App.xaml -> App.xaml。cs -> App -> Main()在解决方案资源管理器中,它会将您带到App. gi。cs源文件,其中包含主()函数。
This file is auto-generated, so rather than editing the Main there, I would recommend creating a new .cs file in your project that contains the Main() function. You then have to change the properties of your project to specify the correct startup object. This is done on the Application tab in your project properties. Set it to the class that contains your custom Main function.
这个文件是自动生成的,所以我建议在项目中创建一个包含Main()函数的.cs文件,而不是编辑主文件。然后必须更改项目的属性以指定正确的启动对象。这是在项目属性中的Application选项卡上完成的。将它设置为包含自定义主函数的类。
You probably want to copy the contains of the autogenerated Main into your new one, since you want your application to behave normally (show the main window, etc.).
您可能希望将自动生成的Main的包含内容复制到新的Main中,因为您希望应用程序正常运行(显示主窗口等)。
#4
2
Entry point is App.xaml.cs typically.
App.xaml入口点。cs一般。
You want to avoid putting code there ideally. Instead try instantiating them in view models for MVVM. It's typically a tricky place to find stuff - as your question is testament to.
您希望避免在理想情况下放置代码。而是尝试在MVVM的视图模型中实例化它们。这通常是一个很难找到东西的地方——正如你的问题所证明的那样。
Another alternative, load them in a helper class and then instantiate that in the app.xaml file.
另一种方法是,在helper类中加载它们,然后在app.xaml文件中实例化它们。