Silverlight 4.0表单初始化问题
我在 Silverlight 4.0 页面之一上遇到一个非常奇怪的错误。我有一个带有“保存”按钮的表单,默认情况下该按钮处于禁用状态。该表单由一堆用户指定的默认值填充,这些默认值来自异步服务器调用(下面的MyFacade.getFormDefaults
)。当用户更改其中一个字段(填充后)时,我希望启用“保存”按钮。
我认为我的逻辑是正确的,但我遇到了一个非常奇怪的错误,我找不到太多有用的信息。错误是:System.InvalidOperationException:对象或值的初始化导致对象或值在完全初始化之前被递归访问。
下面是我所拥有的非常简化的版本...
profile.fs:
type profile() as this =
inherit UriUserControl("/whatever;component/profile.xaml", "profile")
[<DefaultValue>]
val mutable isFormLoaded : bool
[<DefaultValue>]
val mutable btnSave : Button
[<DefaultValue>]
val mutable txtEmail : TextBox
// constructor
do
this.isFormLoaded <- false
// make the "this" values point at the XAML fields
this.btnSave <- this?btnSave
this.txtEmail <- this?txtEmail
// get the form defaults and send them to
MyFacade.getFormDefaults(new Action<_>(this.populateFormDefaults))
()
member this.populateFormDefaults (formDefaults : MyFormDefaultsUIVO array option) =
// populate this.txtEmail with the default value here
this.isFormLoaded <- true // set the form to be loaded once that's done
()
// enable the "Save" button when the user modifies a form field
member this.userModifiedForm (sender : obj) (args : EventArgs) =
// **** EXCEPTION OCCURS ON THE LINE BELOW ****
if this.isFormLoaded then
this.btnSave.IsEnabled <- true
()
profile.xaml:
<nav:Page Name="profile" Loaded="formLoaded">
<TextBox Name="txtEmail" TextChanged="userModifiedForm "/>
<Button Name="btnSave" IsEnabled="False"/>
</nav:Page>
即使我摆脱了所有 isFormLoaded
逻辑,并简单地设置 this.btnSave。 IsEnabled <- true
在 this.userModifiedForm
内部,我得到同样的错误。任何想法将不胜感激。谢谢。
I am getting a very strange error on one of my Silverlight 4.0 pages. I have a form that has a "save" button which is disabled by default. This form gets populated by a bunch of user-specified defaults, which come from an asynchronous server call (MyFacade.getFormDefaults
below). When the user changes one of the fields (after it's populated), I want the "save" button to become enabled.
I think I have the logic correct, but I'm getting a very strange error that I can't find much useful information on. The error is: System.InvalidOperationException: The initialization of an object or value resulted in an object or value being accessed recursively before it was fully initialized.
Below is a very simplified version of what I have...
profile.fs:
type profile() as this =
inherit UriUserControl("/whatever;component/profile.xaml", "profile")
[<DefaultValue>]
val mutable isFormLoaded : bool
[<DefaultValue>]
val mutable btnSave : Button
[<DefaultValue>]
val mutable txtEmail : TextBox
// constructor
do
this.isFormLoaded <- false
// make the "this" values point at the XAML fields
this.btnSave <- this?btnSave
this.txtEmail <- this?txtEmail
// get the form defaults and send them to
MyFacade.getFormDefaults(new Action<_>(this.populateFormDefaults))
()
member this.populateFormDefaults (formDefaults : MyFormDefaultsUIVO array option) =
// populate this.txtEmail with the default value here
this.isFormLoaded <- true // set the form to be loaded once that's done
()
// enable the "Save" button when the user modifies a form field
member this.userModifiedForm (sender : obj) (args : EventArgs) =
// **** EXCEPTION OCCURS ON THE LINE BELOW ****
if this.isFormLoaded then
this.btnSave.IsEnabled <- true
()
profile.xaml:
<nav:Page Name="profile" Loaded="formLoaded">
<TextBox Name="txtEmail" TextChanged="userModifiedForm "/>
<Button Name="btnSave" IsEnabled="False"/>
</nav:Page>
Even if I get rid of all the isFormLoaded
logic, and simply set this.btnSave.IsEnabled <- true
inside of this.userModifiedForm
, I get the same error. Any ideas would be greatly appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当在完全初始化之前访问对象时,F# 运行时会生成异常“对象或值的初始化导致对象或值在完全初始化之前被递归访问”。
在构造函数运行之前访问
this
- 是否检查isFormLoaded
或btnSave.IsEnabled
- 将导致错误。您是否已验证userModifiedForm
仅在构造函数之后调用?The exception - "The initialization of an object or value resulted in an object or value being accessed recursively before it was fully initialized" - is generated by the F# runtime when an object is accessed before being fully initialized.
Accessing
this
- whether to checkisFormLoaded
orbtnSave.IsEnabled
- before the constructor has run will cause the error. Have you verified thatuserModifiedForm
is only called after the constructor?