gpt4 book ai didi

xaml - Silverlight 4.0表单初始化问题

转载 作者:行者123 更新时间:2023-12-02 14:58:04 25 4
gpt4 key购买 nike

我在 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里面, 我犯了同样的错误。任何想法将不胜感激。谢谢。

最佳答案

当在完全初始化之前访问对象时,F# 运行时会生成异常“对象或值的初始化导致对象或值在完全初始化之前被递归访问”。

访问 this - 是否在构造函数运行之前检查 isFormLoadedbtnSave.IsEnabled - 将导致错误。您是否已验证 userModifiedForm 仅在构造函数之后调用?

关于xaml - Silverlight 4.0表单初始化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5223110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com