代码检查:在注册 DependencyProperty 时使用 'nameof' 表达式
此检查建议在注册 DependencyProperty 时使用 nameof 表达式,因为 nameof 表达式使您的代码更易维护且更不易出错。
在下面的示例中,使用 nameof(PageVisibility) 代替 "PageVisibility" 使代码更健壮,并提供属性名称的编译时检查。 如果您稍后重命名 PageVisibility 属性, nameof 表达式将强制您更新参数以反映新名称。 通过这种方式,您可以确保一致性并降低运行时错误的风险。
readonly DependencyProperty PageVisibilityProperty =
DependencyProperty.Register("PageVisibility",
typeof(Visibility),
typeof(PagePreview),
null);
readonly DependencyProperty PageVisibilityProperty =
DependencyProperty.Register(nameof(PageVisibility),
typeof(Visibility),
typeof(PagePreview),
null);
最后修改日期: 2025年 6月 24日