我在Silverlight 4.0页面上遇到一个非常奇怪的错误。我有一个具有“保存”按钮的表单,默认情况下禁用该按钮。此表单由一组用户指定的默认值填充,这些默认值来自异步服务器调用(下面为MyFacade.getFormDefaults
)。当用户更改其中一个字段(填充后)时,我希望“保存”按钮变为启用状态。
我认为我的逻辑是正确的,但我得到一个非常奇怪的错误,我找不到很多有用的信息。错误是:System.InvalidOperationException: The initialization of an object or value resulted in an object or value being accessed recursively before it was fully initialized.
以下是我所拥有的非常简化的版本......
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
,我也会得到同样的错误。任何想法将不胜感激。感谢。
答案 0 :(得分:3)
异常 - “对象或值的初始化导致在完全初始化之前递归访问对象或值” - 在完全初始化之前访问对象时由F#运行时生成。
在构造函数运行之前访问this
- 是否检查isFormLoaded
或btnSave.IsEnabled
将导致错误。您是否已验证userModifiedForm
仅在构造函数后调用?