如何自定义WPF应用程序的启动? [英] How to customize startup of WPF application?

查看:34
本文介绍了如何自定义WPF应用程序的启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建新的 WPF 应用程序项目时,会自动生成 MainWindow.xamlApp.xaml 及其对应的类隐藏代码.在 App.xaml 中有一个属性定义最初运行哪个窗口,默认情况下它是 StartupUri="MainWindow.xaml"

When a new WPF Application project is created, MainWindow.xaml, App.xaml and their corresponding code behind classes are automatically generated. In the App.xaml there is an attribute that defines which window is going to be run initially and by the default it's StartupUri="MainWindow.xaml"

我在同一个项目中创建了一个新的 Dispatcher 类.在启动时,我希望构造该类 Dispatcher 的实例,然后运行其方法之一.该方法实际上会创建并显示 MainWindow 窗口.那么如何修改 App.xamlApp.xaml.cs 以实现它?或者,如果 App 无法实现,我应该如何实现它?谢谢.

I have created a new Dispatcher class in the same project. At startup, I want the instance of that class Dispatcher to be constructed and then one of its method to run. That method would actually create and show the MainWindow window. So how do I modify the App.xaml or App.xaml.cs in order to make it happen? Or, if it cannot be done by App, how should I implement it? Thanks.

推荐答案

您可以从 App.xaml 中删除 StartupUri 属性.

You can remove the StartupUri attribute from the App.xaml.

然后,通过在 App.xaml.cs 中为 OnStartup() 创建覆盖,您可以创建 Dispatcher 类的新实例.

Then, by creating an override for OnStartup() in the App.xaml.cs, you can create your new instance of your Dispatcher class.

这是我的快速 app.xaml.cs 实现的样子:

Here's what my quick app.xaml.cs implementation looks like:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
      base.OnStartup(e);

      new MyClassIWantToInstantiate();
    }
  }
}

更新

我最近发现了此解决方法,如果您使用此方法自定义应用程序启动并且突然没有应用程序可以找到-级别的资源.

I recently discovered this workaround for a bug if you use this method to customize app startup and suddenly none of the Application-level resources can be found.

这篇关于如何自定义WPF应用程序的启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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