属性元数据已经为“时间轴"属性注册 [英] Property Metadata is already registered for “Timeline” property

查看:309
本文介绍了属性元数据已经为“时间轴"属性注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序,该程序由客户端软件调用.第一次工作正常.当我从客户端软件关闭WPF应用程序并再次加载WPF应用程序(而没有在两者之间关闭客户端软件)时,它抛出异常,因为属性元数据已经为时间轴"注册了"属性"的代码如下:

I am having a WPF application which is called by a client software. It works fine for the 1st time.When I closed the WPF application from the client software and again load the WPF app(without closing the client software in between)), it throws an exception as "Property Metadata is already registered for "Timeline" property" for the below code:

Timeline.DesiredFrameRateProperty.OverrideMetadata(typeof(Timeline),
               new FrameworkPropertyMetadata { DefaultValue = 5 });

然后,我在我的应用程序中注释了上面的代码行,并再次重复了上面提到的相同场景,它引发异常,因为Run()中的调用者线程无法访问该对象,因为另一个线程拥有它" ).

And then, I commented the above line of code from my app and again repeated the same scenario which I mentioned above, it throws an exception as "The caller thread cannot access this object because a different thread owns it" in Run().

下面是我在WPF应用程序中使用时间轴"属性的方法.

Below is the method which I am using Timeline property in my WPF application.

public void start()
{
    Timeline.DesiredFrameRateProperty.OverrideMetadata(typeof(Timeline),
       new FrameworkPropertyMetadata { DefaultValue = 5 });
    //Property Metadata is already registered for the "Timeline" property.

    Run();
    // The caller thread cannot access this object because a different thread owns it.
}

推荐答案

您应始终覆盖静态构造函数中的元数据.您不需要为每个实例或方法覆盖.

You should override metadata in static constructor always . You don't need to override with every instance or method.

在您的类的静态构造函数中移动此代码:

Move this code in static constructor of your class:

Timeline.DesiredFrameRateProperty.OverrideMetadata(typeof(Timeline),
               new FrameworkPropertyMetadata { DefaultValue = 5 });

来自 MSDN :

在依赖项属性上覆盖元数据必须先完成 该属性被属性系统使用(这等同于 到注册对象的特定实例的时间为止 属性被实例化). 对OverrideMetadata的调用必须是 在提供该类型的静态构造函数中执行 本身作为OverrideMetadata的forType参数.

Overriding metadata on a dependency property must be done prior to that property being placed in use by the property system (this equates to the time that specific instances of objects that register the property are instantiated). Calls to OverrideMetadata must be performed within the static constructors of the type that provides itself as the forType parameter of OverrideMetadata.

在此处了解更多信息-如何覆盖元数据?

Read more here - How to override metadata?

无法从后台线程修改UI内容,将其放在UI调度程序上是这样的:

You cannot modify UI stuff from background thread, put it on UI dispatcher like this:

App.Current.Dispatcher.Invoke(new Action(() => Run()));

这篇关于属性元数据已经为“时间轴"属性注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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