-->

附加属性:“System.TypeInitializationException”设置默认值时(At

2019-10-19 05:13发布

我想设置我的附加属性的默认值,但是当我做,我得到:

类型的第一个机会异常“System.ArgumentException”发生在WindowsBase.dll

类型的第一个机会异常“System.TypeInitializationException”发生在Oef_AttDepProp.exe

如果没有默认值,东西做工精细。 这是我使用的一些示例代码:

public static readonly DependencyProperty IsEigenaarProperty = DependencyProperty.RegisterAttached(
"Eigenaar", typeof(clsPersoon), typeof(UIElement), 
new UIPropertyMetadata(new clsPersoon("test", "test"), PropertyChanged));

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Browsable(true)]
public clsPersoon Eigenaar
{
 get
 {
  return _persoon;
 }
 set
 {
  _persoon = value;
 }
}

public static void SetEigenaar(UIElement element, clsPersoon value)
{
 element.SetValue(IsEigenaarProperty, value);
}

public static clsPersoon GetEigenaar(UIElement element)
{
 return (clsPersoon)element.GetValue(IsEigenaarProperty);
}

private static void PropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
 if (obj is Window1)
  ((Window1)obj).Title = GetEigenaar(((Window1)obj)).ToString();
}

这是“新clsPersoon(”测试“‘测试’)”,这似乎是原因的问题,但是,这只是一个非常简单的类有2串构造函数。

编辑 :当试图通过点击事件来设置该属性,而不是window_load,我得到的的InnerException:“默认值的‘Eigenaar’属性不能被绑定到特定线程”

Answer 1:

通常类型的异常TypeInitializationException时异常的静态构造函数时抛出。 看这里。

此外,从内部异常:

默认值Eigenaar属性不能绑定到特定线程。

这通常意味着你的财产是不是线程安全的(例如,不继承System.Windows.Freezable )。 检查此线程的血淋淋的细节和MSDN有关依赖属性的默认值的详细信息。



文章来源: Attached Property: 'System.TypeInitializationException' when setting default value