# 基础篇 ### 依赖属性propdb ```c# public int MyProperty { get { return (int)GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } } // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(int), typeof(ownerclass), new PropertyMetadata(0)); ``` ### 附加属性propa ```c# public static int GetMyProperty(DependencyObject obj) { return (int)obj.GetValue(MyPropertyProperty); } public static void SetMyProperty(DependencyObject obj, int value) { obj.SetValue(MyPropertyProperty, value); } // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.RegisterAttached("MyProperty", typeof(int), typeof(ownerclass), new PropertyMetadata(0)); ``` ## xaml资源键类型
定义方式 | 优点 | 缺点 | 适用场景 |
---|---|---|---|
**普通字符串键** | 简单直观,易于使用 | 命名冲突风险,无法跨程序集共享 | 小型项目,局部资源 |
**类型键** | 自动应用,无需显式引用 | 灵活性较低 | 全局样式,基础样式复用 |
**静态资源键** | 强类型支持,可维护性高 | 定义稍复杂 | 大型项目,组件化开发 |
**`ComponentResourceKey`** | 跨程序集支持,语义化标识 | 定义和使用复杂 | 组件库开发,主题或样式库 |
**动态资源键** | 动态绑定,灵活性高 | 性能开销 | 主题切换,多语言支持 |