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

发布于 2024-08-18 21:44:54 字数 1274 浏览 10 评论 0 原文

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

WindowsBase.dll 中发生了“System.ArgumentException”类型的第一次机会异常

Oef_AttDepProp.exe 中第一次出现“System.TypeInitializationException”类型异常

如果没有默认值,一切工作正常。 这是我使用的一些示例代码:

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 个字符串构造函数的非常简单的类。

编辑:当尝试通过单击事件而不是 window_load 设置属性时,我收到以下内部异常:“‘Eigenaar’属性的默认值无法绑定到特定线程。”

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

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

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

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.

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."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必你懂 2024-08-25 21:44:54

通常,当静态构造函数中发生异常时,会引发 TypeInitializationException 类型的异常。看看那里。

另外,从内部异常来看:

Eigenaar 属性的默认值无法绑定到特定线程。

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

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

Also, from the inner exception:

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文