[UWP] [VB] App Class中字段的初始化顺序&自定义类型中的构造函数 [英] [UWP][VB]Initialization Order for Fields in App Class & Constructors in Custom Types

查看:81
本文介绍了[UWP] [VB] App Class中字段的初始化顺序&自定义类型中的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UWP应用,在App类中有一个如下定义的字段:

 Public Shared ReadOnly Routes As Route()= {New Route(), New Route(),New Route()} 


我的MainPage类的Loaded事件中使用的类型的构造函数使用:

 App.Routes.Length 


但是,我收到了一个  TypeInitializationException,因为在初始化Routes之前使用了带有App.Routes.Length的构造函数。有没有什么办法可以强制路由在我使用它的类型的构造函数之前被初始化?它是ReadOnly,
所以我几乎不得不在声明中初始化它。有任何想法吗?谢谢。



Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

解决方案

< blockquote>我尝试将初始化移动到App的构造函数,如下所示:

 Public Shared Routes As Route()

Public Sub New()
InitializeComponent()
App.Routes = {New Route(),New Route(),New Route()}
End Sub


这个牺牲ReadOnly(虽然在这一点上,我可以设法处理),并在其他构造函数之前调用,但是给了我一个ArgumentNullException(在第二行),说App.Routes是Nothing。它是,但我希望它是,
是为什么我想为它分配一个值。是否有一些新规则我不允许再为null变量赋值?感谢。

I have a UWP app that has a field defined as follows in the App class:

Public Shared ReadOnly Routes As Route() = {New Route(), New Route(), New Route()}

And the constructor for a type used in the Loaded event of my MainPage class uses:

App.Routes.Length

However, I receive a TypeInitializationException because the constructor with App.Routes.Length is used before Routes is initialized. Is there any way I can force Routes to be initialized before the constructor for my type that uses it? It is ReadOnly, so I pretty much have to initialize it at the declaration. Any ideas? Thanks.


Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

解决方案

I tried moving the initialization to the constructor for App as follows:

Public Shared Routes As Route()

Public Sub New()
	InitializeComponent()
	App.Routes = {New Route(), New Route(), New Route()}
End Sub

This sacrifices the ReadOnly (although at this point, I can manage to deal with that), and is called before the other constructors, but gives me an ArgumentNullException (on the second line) saying that App.Routes is Nothing. It is, but I expect it to be, that is why I want to assign a value to it. Is there some new rule that I'm not allowed to assign a value to a null variable anymore? Thanks.


这篇关于[UWP] [VB] App Class中字段的初始化顺序&amp;自定义类型中的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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