Problem initializing a Silverlight 4.0 form

I get a very strange error on one of my Silverlight 4.0 pages. I have a form in which there is a Save button that is disabled by default. This form is populated with a variety of user-defined defaults that come from an asynchronous server call ( MyFacade.getFormDefaults below). When the user changes one of the fields (after filling it out), I want the "Save" button to turn on.

I think that I have the correct logic, but I get a very strange error about which I can not find a lot of useful information. Error: 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 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 just put this.btnSave.IsEnabled <- true inside this.userModifiedForm , I get the same error. Any ideas are greatly appreciated. Thanks.

+4
source share
1 answer

The exception - "Initialization of an object or value caused by recursive access to the object or value before its full initialization" - is generated by the F # runtime when the object is accessed before full initialization.

Access to this - checking isFormLoaded or btnSave.IsEnabled - before starting the constructor will result in an error. Have you confirmed that userModifiedForm is called only after the constructor?

+3
source

Source: https://habr.com/ru/post/1342673/


All Articles