类型long的依赖属性中的错误。

时间:2021-12-15 16:30:27

I have an "Id" dependency property declared:

我有一个“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));

"Component" is a User Control that have "Id".

“组件”是具有“Id”的用户控件。

When I run the application it gives me an exeption:

当我运行应用程序时,它会给我一个感受:

An unhandled exception of type 'System.TypeInitializationException' occurred in PresentationCore.dll

类型系统的未处理异常。TypeInitializationException”内发生在PresentationCore.dll

Additional information: The type initializer for 'My_Program.Component' threw an exception.

附加信息:“My_Program”的类型初始化器。组件的抛出一个异常。

If i change the type "long" to "int" everything works fine. What is the problem? can't I create a dependency property of type "long"?

如果我将“long”类型改为“int”类型,一切正常。这个问题是什么?我不能创建一个“long”类型的依赖属性吗?

Thank you for your help.

谢谢你的帮助。

2 个解决方案

#1


4  

Try casting the default value to long:

尝试将默认值转换为long:

new PropertyMetadata((long)-1);

#2


4  

Try using suffix L, i.e.: -1L

尝试使用后缀L,也就是。l:1

#1


4  

Try casting the default value to long:

尝试将默认值转换为long:

new PropertyMetadata((long)-1);

#2


4  

Try using suffix L, i.e.: -1L

尝试使用后缀L,也就是。l:1