Qwe*_*tie 54 .net wpf entry-point
WPF定义了自己的Main()方法.我应该如何用我自己的Main方法替换它(通常)打开WPF MainWindow(例如通过命令行参数添加非WPF脚本模式)?
Joe*_*ant 53
一些示例描述了将App.xaml的构建操作从更改ApplicationDefinition到Page编写自己的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中保存的资源.
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)
相关的部分是我从改变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();
}
}
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:
使用您的自定义静态 Main 方法创建新类。在此方法的最后,只需调用 WPF 生成的原始 App.Main() 即可:
\n\npublic class Program\n{\n [STAThread]\n public static void Main(string[] args)\n {\n // Your initialization code\n App.Main();\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n然后将您的project\xe2\x80\x99s \xe2\x80\x9cStartup object\xe2\x80\x9d 设置设置为包含静态Main() 的类。
\n