我声明了一个“Id”依赖属性:
public long Id
{
get { return (long)GetValue(IdProperty); }
set { SetValue(IdProperty, value); }
}
public static readonly DependencyProperty IdProperty =
DependencyProperty.Register("Id", typeof(long),
typeof(Component), new PropertyMetadata(-1));
“组件”是具有“Id”的用户控件。
当我运行应用程序时,它给我一个异常:
An unhandled exception of type 'System.TypeInitializationException' occurred in PresentationCore.dll
Additional information: The type initializer for 'My_Program.Component' threw an exception.
如果我将类型“long”更改为“int”,一切正常。 问题是什么?我不能创建“long”类型的依赖属性吗?
感谢您的帮助。
最佳答案
尝试将默认值转换为 long:
new PropertyMetadata((long)-1);
关于c# - long 类型的依赖属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7010563/