[UWP]依赖属性2:使用依赖属性
5. 完整的自定义依赖属性5.1 定义/// <summary> /// 标识 Title 依赖属性。 /// </summary> public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(MyPage), new PropertyMetadata(string.Empty)); /// <summary> /// 获取或设置Content的值 /// </summary> public object Content { get { return (object)GetValue(ContentProperty); } set { SetValue(ContentProperty, value); } } /// <summary> /// 标识 Content 依赖属性。 /// </summary> public static readonly DependencyProperty ContentProperty = DependencyProperty.Register("Content", typeof(object), new PropertyMetadata(null, OnContentChanged)); private static void OnContentChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) { MyPage target = obj as MyPage; object oldValue = (object)args.OldValue; object newValue = (object)args.NewValue; if (oldValue != newValue) target.OnContentChanged(oldValue, newValue); } protected virtual void OnContentChanged(object oldValue, object newValue) { } 以上代码为一个相对完成的依赖属性例子(还有一些功能比较少用就不写出了),从这段代码可以看出,自定义依赖属性的步骤如下:
5.2 代码段注册依赖属性的语法比较难记,可以使用VisualStudio自带的代码段propdp(输入propdp后按两次tab)自动生成,这个代码段生成的代码只有基本功能,如下所示: 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), new PropertyMetadata(0)); 要生成完整的依赖属性代码,可以使用自定义的代码段,以下代码段生成的就是完整的依赖属性定义,快捷键是dp: <?xml version="1.0" encoding="utf-8"?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Keywords> <Keyword>dp</Keyword> </Keywords> <SnippetTypes> <SnippetType>SurroundsWith</SnippetType> </SnippetTypes> <Title>Dependency Property</Title> <Author>dino.c</Author> <Description>For Dependency Property</Description> <HelpUrl> </HelpUrl> <Shortcut>dp</Shortcut> </Header> <Snippet> <References> <Reference> <Assembly> </Assembly> </Reference> </References> <Declarations> <Literal Editable="true"> <ID>int</ID> <ToolTip>int</ToolTip> <Default>int</Default> <Function> </Function> </Literal> <Literal Editable="true"> <ID>MyProperty</ID> <ToolTip>属性名</ToolTip> <Default>MyProperty</Default> <Function> </Function> </Literal> <Literal> <ID>defaultvalue</ID> <ToolTip>默认值</ToolTip> <Default>0</Default> </Literal> <Literal Editable="false"> <ID>classname</ID> <ToolTip>类名</ToolTip> <Function>ClassName()</Function> <Default>ClassNamePlaceholder</Default> </Literal> </Declarations> <Code Language="csharp" Kind="method body"> <![CDATA[ /// <summary> /// 获取或设置$MyProperty$的值 /// </summary> public $int$ $MyProperty$ { get { return ($int$)GetValue($MyProperty$Property); } set { SetValue($MyProperty$Property,value); } } /// <summary> /// 标识 $MyProperty$ 依赖属性。 /// </summary> public static readonly DependencyProperty $MyProperty$Property = DependencyProperty.Register("$MyProperty$",typeof($int$),typeof($classname$),new PropertyMetadata($defaultvalue$,On$MyProperty$Changed)); private static void On$MyProperty$Changed(DependencyObject obj,DependencyPropertyChangedEventArgs args) { $classname$ target= obj as $classname$; $int$ oldValue = ($int$)args.OldValue; $int$ newValue = ($int$)args.NewValue; if (oldValue != newValue) target.On$MyProperty$Changed(oldValue,newValue); } protected virtual void On$MyProperty$Changed($int$ oldValue,$int$ newValue) { }]]> </Code> </Snippet> </CodeSnippet> </CodeSnippets> 6. Slider与OneWayBinding的"Bug"UWP的依赖属性比起WPF有了大幅简化,需要学习的地方少了很多,但是功简化了也不一定是一件好事。譬如下面这个代码: <StackPanel> <Slider x:Name="SliderSource" Maximum="200" /> <Slider x:Name="SliderTarget" Maximum="100" Value="{Binding ElementName=SliderSource,Path=Value,Mode=OneWay}" /> </StackPanel> 理想的情况下,拖动SliderSource到100以后,因为SliderTarget的Value已经超过Maximum设置的100,所以没有反应;再次把SliderSource拖动到100以下,SliderTarget才会重新跟随SliderSource改变。但实际上,之后无论再怎么拖动SliderSource,SliderTarget都不会有反应。 private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { RangeBase range = d as RangeBase; double minimum = range.Minimum; double maximum = range.Maximum; double value = (double)e.NewValue; double initialVal = (double)e.OldValue; if (value < minimum) { SetValue(ValueProperty, minimum); } if (value > maximum) { SetValue(ValueProperty, maximum); } range.OnValueChanged(initialVal, value); } 实际的代码要复杂些,但基本的逻辑就是这样。如果新的Value值超过了Maximum或Minimum,就将Value重新设置为Maximum或Minimum,保证Value不会超过设定的范围。在这个例子里,如果在这个函数开头的位置调用 range.ReadLocalValue(range.ValueProperty),返回的是一个Binding,在结尾的位置调用,返回的则是double类型的100,因为这段代码将Value由OneWay Binding覆盖为maximum的double值了。 在WPF中,这个问题并不存在,因为WPF的依赖属性可以使用CoerceValueCallback约束属性值,而UWP的依赖属性被简化了,缺少这个功能。可以在网上用“Silverlight CoerceValue Helper”或“Silverlight CoerceValue Utils”等关键字试试搜索一些解决方案。为什么使用Silverlight的关键字来搜索?因为Silverlight同样存在这个问题。虽然网上能找到不少解决方案,但以我的经验来说没有方案能很好地解决这个问题。最后我的解决方案如下: /// <summary> /// 不要使用OneWayBinding,只能使用TwoWayBinding /// </summary> public object MyProperty { get { return (object)GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } } 反正不是写控件库给别人用,写个注释并且和同事打声招呼就算了。 有兴趣的话可以参考Silverlight RangeBase的源代码,由于Silverlight和UWP比较接近,参考Silverlight的源码基本就可以理解RangeBase的实现细节。
7.参考依赖属性概述 自定义依赖属性 Silverlight 依赖项属性概述 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |