开发者

Silverlight 4.0 Form Initialization Problem

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 <- t开发者_运维技巧his?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.


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 check isFormLoaded or btnSave.IsEnabled - before the constructor has run will cause the error. Have you verified that userModifiedForm is only called after the constructor?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜