wpf 已为“时间线”属性注册了属性元数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20897434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:23:08  来源:igfitidea点击:

Property Metadata is already registered for “Timeline” property

wpfmultithreadingtimeline

提问by Sathyanarayanan

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:

我有一个由客户端软件调用的 WPF 应用程序。第一次运行正常。当我从客户端软件关闭 WPF 应用程序并再次加载 WPF 应用程序(中间没有关闭客户端软件))时,它会抛出一个异常,因为“属性元数据已经为“时间线”注册了属性”用于以下代码:

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

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().

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

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

下面是我在 WPF 应用程序中使用 Timeline 属性的方法。

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

回答by Rohit Vats

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

From MSDN:

MSDN

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 constructorsof the type that provides itself as the forType parameter of OverrideMetadata.

必须在属性系统使用该属性之前覆盖依赖属性上的元数据(这相当于实例化注册该属性的对象的特定实例的时间)。必须在将自身提供为 OverrideMetadata 的 forType 参数的类型的静态构造函数执行对 OverrideMetadata 的调用

Read more here - How to override metadata?

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



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

不能从后台线程修改 UI 的东西,把它放在 UI 调度程序上,如下所示:

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