设置默认值时的附加属性:"System.TypeInitializationException" [英] Attached Property: 'System.TypeInitializationException' when setting default value

查看:79
本文介绍了设置默认值时的附加属性:"System.TypeInitializationException"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to set a default value of my Attached Property, but when I do I get:

WindowsBase.dll中出现了'System.ArgumentException'类型的第一次机会异常

A first chance exception of type 'System.ArgumentException' occurred in WindowsBase.dll

Oef_AttDepProp.exe中发生了类型为'System.TypeInitializationException'的第一次机会异常

A first chance exception of type 'System.TypeInitializationException' occurred in Oef_AttDepProp.exe

没有默认值,一切正常. 这是我使用的一些示例代码:

Without the default value, things work fine. This is some sample code I used:

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();
}

它是"new clsPersoon("test","test"))",这似乎是导致问题的原因,但这只是一个非常简单的类,带有2字符串构造函数.

It's the "new clsPersoon("test", "test")" which seems to be the cause the problem, but that is only a very simple class with a 2-string-constructor.

编辑:当尝试通过click事件而不是window_load设置属性时,我收到一个innerException:"Eigenaar"属性的默认值无法绑定到特定线程."

Edit: When trying to set the property through a click event, instead of the window_load, I get an innerException of: "Default value for the 'Eigenaar' property cannot be bound to a specific thread."

推荐答案

当静态构造函数中发生异常时,通常会抛出类型为TypeInitializationException的异常.看那里.

Typically exceptions of type TypeInitializationException are thrown when an exception occurs in the static constructor. Look there.

此外,从内部异常来看:

Also, from the inner exception:

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

Default value for the Eigenaar property cannot be bound to a specific thread.

这通常意味着您的属性不是线程安全的(例如,不继承自 thread 获取详细信息,并 MSDN 有关依赖项属性的默认值的详细信息.

This usually means that your property is not thread-safe (e.g., doesn't inherit from System.Windows.Freezable). Check this thread for gory details and MSDN for details about default values for dependency properties.

这篇关于设置默认值时的附加属性:"System.TypeInitializationException"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆