简体   繁体   English

我正在创建一个类型为 Window 的依赖属性。 有人可以告诉我需要使用哪个 FrameworkPropertyMetadataOptions 标志吗

[英]I am creating a dependency property whose type is Window. Could someone tell me which FrameworkPropertyMetadataOptions flag I need to use

I am trying to figure out which FrameworkPropertyMetadataOptions flag to use for Window type.我试图找出用于 Window 类型的FrameworkPropertyMetadataOptions标志。

public static readonly DependencyProperty RootWindowProperty;公共 static 只读 DependencyProperty RootWindowProperty;

static VideoPlayer() { FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata(new Window(), ...); static VideoPlayer() { FrameworkPropertyMetadata 元数据 = new FrameworkPropertyMetadata(new Window(), ...); // I don't know which flags to use here RootWindowProperty = DependencyProperty.Register( "RootWindow", typeof(Window), typeof(VideoPlayer), metadata); // 我不知道这里使用哪些标志 RootWindowProperty = DependencyProperty.Register( "RootWindow", typeof(Window), typeof(VideoPlayer), metadata);

} }

Note, that it is best practice to use field initializers (inline) instead of static constructors (see example below).请注意,最好使用字段初始值设定项(内联)而不是 static 构造函数(参见下面的示例)。
The runtime is able to optimize the performance of types that don't have an explicitly defined static constructor.运行时能够优化没有明确定义的 static 构造函数的类型的性能。

You don't have to set any meta data flags:您不必设置任何元数据标志:

class VideoPlayer : DependencyObject
{
  public static readonly DependencyProperty RootWindowProperty = DependencyProperty.Register(
    "RootWindow", 
    typeof(Window),
    typeof(VideoPlayer),
    new PropertyMetadata(default));

  public Window RootWindow
  {
    get => (Window)GetValue(RootWindowProperty);
    set => SetValue(RootWindowProperty, value); 
  }
}

See Microsoft Docs: Custom Dependency Properties to learn about Setting Appropriate Metadata Flags .请参阅Microsoft Docs:自定义依赖属性以了解设置适当的元数据标志

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可以告诉我是否可以在C#中使用最新的OpenGL? - could tell me if I can use the latest OpenGL in C#? 有人可以告诉我如何从ItemsGrouped集合中删除项目吗? - Could someone tell me how do I remove an item from the ItemsGrouped collection? 谁能告诉我我在摘要中犯错了吗 - Can anyone tell me were I am making mistake in the snippet 如何判断我正在运行哪个WCF版本? - How do I tell which WCF version I am running? 使用XMLTextReader,如何知道我在哪个元素上? - Using XMLTextReader, how can I tell which element I am on? 如何可靠地知道当前(焦点)展望窗口中的对象。 即资源管理器类型或检查器类型 - How to reliably know what object is current (in focus) outlook window. i.e. an explorer type or Inspector type 有人能告诉我这段代码使用的是哪个版本的 Angular 吗? - Can someone tell me which version of Angular is this code using? 有人可以告诉我在 Xamarin 中绑定哪里出了问题 - Can someone tell me where I'm going wrong with Binding in Xamarin 有人可以解释我如何使用Command(Action,CanExecute) - Can someone explain me how I use Command(Action, CanExecute) 为什么我的依赖属性会出现异常? - Why am I getting an exception on my dependency property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM