如何在 WPF 应用程序中使用启动事件? [英] How to use startup event in WPF app?

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

问题描述

我已经尝试了这两个示例来在 WPF 应用程序中使用启动事件:

I've tried both of these examples to use a startup event in a WPF app:

http://www.wpf-tutorial.com/wpf-application/working-with-app-xaml/

https://msdn.microsoft.com/en-us/library/system.windows.application.startup(v=vs.110).aspx

每一个在partial App类中创建一个方法:

Each one creates a method in the partial App class:

void Application_Start(object sender, StartupEventArgs args){
  //do something
}

并在 App.xaml 中指向它:

and in App.xaml, point to it:

Startup = "Application_Start"

我总是收到这个构建错误:

I always get this build error:

MyApp.App 不包含 Application_Start 的定义,也没有扩展方法 Application_Start 接受类型的第一个参数可以找到 MyApp.App.

MyApp.App does not contain a definition for Application_Start and no extension method Application_Start accepting a first arument of type MyApp.App could be found.

知道我做错了什么吗?

推荐答案

你应该在参数类型中使用 StartupEventArgs 而不是 StartEventArgs :

You should use StartupEventArgs instead of StartEventArgs in parameter type:

void Application_Start(object sender, StartupEventArgs args)
{
    //do something           
}

您应该使用与主 App 类相同的命名空间创建分部类:

You should create your partial class with the same namespace as your main App class:

namespace WpfAppStartEvent
{
    public partial class App : Application
    {

    }
}

namespace WpfAppStartEvent
{
    public partial class App 
    {
        void Application_Start(object sender, StartupEventArgs args)
        {
            //do something           
        }
    }
}

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

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