gpt4 book ai didi

wpf - 属性元数据已为 “Timeline” 属性注册

转载 作者:行者123 更新时间:2023-12-04 01:32:05 26 4
gpt4 key购买 nike

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

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

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

下面是我在 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.
}

最佳答案

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

将此代码移动到类的静态构造函数中:

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

来自 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.



在这里阅读更多 - How to override metadata?

你不能从后台线程修改 UI 的东西,把它放在 UI 调度程序上,如下所示:
App.Current.Dispatcher.Invoke(new Action(() => Run()));

关于wpf - 属性元数据已为 “Timeline” 属性注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20897434/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com