如何在C#应用程序启动时运行的代码吗? [英] How to run code right when a C# application starts?

查看:428
本文介绍了如何在C#应用程序启动时运行的代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经把一些代码市民主窗口(){}里面,但我一直当我这样做(不是我的计算机上,但3人我试过它得到一些模糊的XAML解析错误 - 是的! )

I have put some code inside of the public MainWindow() {} but I kept getting some obscure XAML parsing errors as soon as I did that (not on my computer but on 3 others I've tried it on - yep!)

是否有尽快T他应用程序启动运行代码的首选方式?

Is there the preferred way to run code AS SOON as t he application starts?

该理论我希望它给家里打电话,问它,它是确定启动。如果不是的话,我希望应用程序关闭了。说它是一个临时搭建的复制保护:)

The theory is I want it to call home and ask it it's ok to start. If it's not, I want the app to close out. Call it a makeshift copy-protection :)

推荐答案

在正常情况下,WPF创建方法(应用程序的入口点)为您。您的选择

Under normal circumstances, WPF creates the Main method (the entrypoint of the application) for you. Your options


  • 创建为 Application.Startup 事件,并把你的代码中有。或者,您可以覆盖 OnStartup() 方法。

  • 如果这是太晚了你,把你的代码中的应用的参构造器(它可能不存在,但你可以创建一个)。

  • 如果连这是为时已晚,你可以创建自己的的Main()方法。 有几种方法如何做到这一点。也许最简单的就是把它放在另一个类,并告诉Visual Studio中要在项目的属性来使用这个方法。

  • Create a handler for the Application.Startup event and put your code there. Alternatively, you can override the OnStartup() method.
  • If that's too late for you, put your code in the App's parameterless constructor (it probably doesn't exist, but you can create it).
  • If even that's too late, you can create your own Main() method. There are several ways how to do that. Probably the easiest is to put it in another class and tell Visual Studio you want to use this method in the project's properties.

在该另一方面,你说你得到一些不起眼的XAML解析错误。也许你应该弄清楚究竟是什么意思呢?

On the other hand, you said you're getting some obscure XAML parsing errors. Maybe you should figure out what exactly do they mean?

这篇关于如何在C#应用程序启动时运行的代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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