在哪里初始化我的应用程序的组件? [英] Where to initialize my application's components?

查看:56
本文介绍了在哪里初始化我的应用程序的组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用wpf,我想知道应该将初始化代码放在哪里?我需要初始化一些变量和自定义类的实例...

I am just getting started with wpf and I am wondering where I should put the initialization code for my application? I need to initialize some variables and instances of custom classes...

启动wpf项目时,我有一个App.xaml.cs和MainWindow.xaml.cs fils.我应该在哪里放置初始化代码,应该使用一些挂钩函数吗?

When I start a wpf project I have a App.xaml.cs and a MainWindow.xaml.cs fils. Whereabout should I put my initialization code, is there some hook function I am supposed to use?

推荐答案

如果仅在Mainwindow.cs中使用自定义分类,变量等,则只需在其中声明它们.如果要声明将在整个应用程序(而不仅仅是Mainwindow)中使用的全局对象等,则可以在app.xaml.cs中声明它们.

If your custom clases, variables etc are only to be used in Mainwindow.cs then just declare them in there. If you want to declare global objects etc that are to be used throughout thwe entire application (not just Mainwindow) then yes declare them in app.xaml.cs.

 

您可以通过声明以下方法- 

You can hook into the OnStartup event by declaring the following method - 

 

protected override void OnStartup(StartupEventArgs e)

//create your global variables object etc in this method 


I hope that answers your question?


这篇关于在哪里初始化我的应用程序的组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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