应用程序 OnStartup 永远不会被调用
非常简单的问题,但我没有取得任何进展,所以我想我应该问...
我正在编写一个小的 WPF 原型,我在其中放置了引导 我认为它所属的逻辑:在(重写的)App.OnStartup 方法中。
问题是该方法永远不会被调用,我不知道为什么!
我浏览了一下,发现有人说 App.xaml 中的
标记必须在“x:Class<”中指定实现类 (
App
) /代码>”属性。我将其从 x:Class="Application"
更改为 x:Class="App"
但没有什么区别。
我在这里缺少什么?
编辑: 这是代码...
XAML:
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="App"
ShutdownMode="OnMainWindowClose"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Recources\Brushes\Brushes.xaml"/>
<ResourceDictionary Source="Recources\Templates\Templates.xaml"/>
<ResourceDictionary Source="Recources\Styles\GVSStyles.xaml"/>
<ResourceDictionary Source="Recources\Styles\TimePicker.xaml"/>
<ResourceDictionary Source="Recources\Icons\GVSIcons.xaml"/>
<ResourceDictionary Source="Recources\Icons\BottleIcon.xaml"/>
<ResourceDictionary Source="Recources\Styles\BusyAnimationStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
背后的代码...
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// lower default framerate from 60 to 20 to save CPU ...
Timeline.DesiredFrameRateProperty.OverrideMetadata(
typeof(Timeline),
new FrameworkPropertyMetadata { DefaultValue = 20 });
hookUpViews();
connectToServer();
}
Very simple problem but I'm making no progress so I thought I should ask...
I'm writing a small WPF prototype where I placed the boot
up logics where I believe it belongs: In (the overridden) App.OnStartup method.
The problem is the method never gets called and I have no idea why!
I browsed around some and found someone saying the <Application>
tag in App.xaml must specify the implementing class (App
) in the "x:Class
" attribute. I changed it from x:Class="Application"
to x:Class="App"
but it made no difference.
What am I missing here?
EDIT:
Here's the code...
XAML:
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="App"
ShutdownMode="OnMainWindowClose"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Recources\Brushes\Brushes.xaml"/>
<ResourceDictionary Source="Recources\Templates\Templates.xaml"/>
<ResourceDictionary Source="Recources\Styles\GVSStyles.xaml"/>
<ResourceDictionary Source="Recources\Styles\TimePicker.xaml"/>
<ResourceDictionary Source="Recources\Icons\GVSIcons.xaml"/>
<ResourceDictionary Source="Recources\Icons\BottleIcon.xaml"/>
<ResourceDictionary Source="Recources\Styles\BusyAnimationStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Code behind...
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// lower default framerate from 60 to 20 to save CPU ...
Timeline.DesiredFrameRateProperty.OverrideMetadata(
typeof(Timeline),
new FrameworkPropertyMetadata { DefaultValue = 20 });
hookUpViews();
connectToServer();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑:您的 XAML 似乎与后面的代码无关,
x:Class
需要包含您的 App 类的命名空间。例如MyWpfApplication.App
。除非您发布一些代码,否则您只会胡乱猜测,这是我的:您没有正确覆盖该方法,而是使用相同名称和签名的方法隐藏它。
这就是工作覆盖的样子:
按照建议,您可以使用
Startup
事件,但您不必这样做,进一步StartupUri
除了代码之外还将执行在覆盖中。Edit: Your XAML seems to not be associated with the code behind, the
x:Class
needs to include the namespace of your App class. e.g.MyWpfApplication.App
.Unless you post some code you just get wild guessing, here's mine: You didn't properly override the method but hide it with a method of the same name and signature.
This is what a working override should look like:
As suggested you can use the
Startup
event instead, but you don't have to, further theStartupUri
will be executed in addition to the code in the override.您需要连接 EventHandler :
You need the connect the EventHandler :
就我而言,我重命名了项目和命名空间。然后后面代码中的所有方法都不再触发。
x:Class
仍然显示旧的命名空间。App
类只是引用另一个不再使用的命名空间,对此没有任何抱怨。因此,简而言之,您必须重命名
x:Class
,就像对“常规”代码文件所做的那样。In my case I renamed the project and the namespace. Then all methods in the code behind didn't fire any more.
The
x:Class
still showed the old namespace. TheApp
class was just referring another namespace what was out of use, and nothing complained about that.So, you make short, you have to rename the
x:Class
, just the way you would do for 'regular' code files.作为 @Philippe 答案的替代方案,您还可以在代码隐藏中进行连接:
As an alternative to @Philippe's answer, you can also wire up in the code-behind: