在WPF中重写OnStartup [英] override OnStartup in WPF
问题描述
由于某种原因,我根本无法使它正常工作.我从各种来源读到了可以在WPF应用程序中覆盖OnStartup的信息,它将在创建应用程序时触发.但是,无论我做什么,都没有任何反应.这是代码.
For some reason I can't get this to work at all. I have read from various sources that I can override OnStartup in a WPF application and it will fire off as the App is created. However, no matter what I do, nothing is happening. Here is the code.
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
// My code goes here, but nothing ever happens.
base.OnStartup(e);
}
}
很明显,我缺少了一些东西.遗憾的是,MSDN页面也没有提供太多见识. http://msdn.microsoft.com/en-us/library/system.windows.application.onstartup.aspx
Obviously I am missing something. Sadly the MSDN page doesn't offer much insight either. http://msdn.microsoft.com/en-us/library/system.windows.application.onstartup.aspx
我在做什么错了?
原来,我的问题是名称空间中的一个小错字.App.xaml.cs的类定义为"RTDMyApp.App",而App.xaml文件将其称为"RTD_MYApp.App",无论如何,这个事实与下面接受的答案相结合,使我回到了正轨
It turns out that my problem was a small typo in the namespace. App.xaml.cs had the class defined as 'RTDMyApp.App' and the App.xaml file was referring to it as 'RTD_MYApp.App' At any rate, this fact, combined with the accepted answer below has gotten me back on track.
推荐答案
我认为您真正想做的是订阅到 Startup
事件.您可以在XAML文件中执行此操作:
I think what you really want to do is to subscribe to the Startup
event. You can do this in your XAML file:
<Application ... Startup="Application_Startup">
这篇关于在WPF中重写OnStartup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!