c# - long 类型的依赖属性错误

标签 c# .net wpf dependency-properties

我声明了一个“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/

相关文章:

c# - 如何连接到 Windows 8.1 上的 Azure Blob 存储?

c# - Visual Studio 不会在崩溃时进行调试

c# - Windows Azure 云服务 - 访问已部署的 xml 文件被拒绝

c# - 来自 LINQ SQL 的 List<T> 的随机元素

WPF MVVM - 如何将服务注入(inject) ViewModel

c# - 在调试托管代码时调试非托管代码

c# - 通过 WCF 自托管 2000 多个 Web 服务

.net - 如何在 Entity Framework 中使用外键作为复合主键的一部分?

c# - 如何保证我的 WPF 应用程序的永久管理员权限

c# - 使用单个 TextBlock 来显示错误消息?