应用程序 OnStartup 永远不会被调用

发布于 2024-11-16 10:48:16 字数 1933 浏览 3 评论 0原文

非常简单的问题,但我没有取得任何进展,所以我想我应该问...

我正在编写一个小的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幸福不弃 2024-11-23 10:48:16

编辑:您的 XAML 似乎与后面的代码无关,x:Class 需要包含您的 App 类的命名空间。例如MyWpfApplication.App


除非您发布一些代码,否则您只会胡乱猜测,这是我的:您没有正确覆盖该方法,而是使用相同名称和签名的方法隐藏它。

这就是工作覆盖的样子:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    MessageBox.Show("!");
}

按照建议,您可以使用 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:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    MessageBox.Show("!");
}

As suggested you can use the Startup event instead, but you don't have to, further the StartupUri will be executed in addition to the code in the override.

云淡月浅 2024-11-23 10:48:16

您需要连接 EventHandler :

<Application x:Class="Abc.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="Application_Startup">

You need the connect the EventHandler :

<Application x:Class="Abc.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="Application_Startup">
七堇年 2024-11-23 10:48:16

就我而言,我重命名了项目和命名空间。然后后面代码中的所有方法都不再触发。

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. The App 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.

花辞树 2024-11-23 10:48:16

作为 @Philippe 答案的替代方案,您还可以在代码隐藏中进行连接:

public App()
{
  this.Startup += new StartupEventHandler(App_Startup);
}

void App_Startup(object sender, StartupEventArgs e)
{
   //do stuff here...
}

As an alternative to @Philippe's answer, you can also wire up in the code-behind:

public App()
{
  this.Startup += new StartupEventHandler(App_Startup);
}

void App_Startup(object sender, StartupEventArgs e)
{
   //do stuff here...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文