Property Metadata is already registered for “Timeline” property

狂风中的少年 提交于 2019-12-08 15:28:50

问题


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

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

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

回答1:


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:

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?


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

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


来源:https://stackoverflow.com/questions/20897434/property-metadata-is-already-registered-for-timeline-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!