各位好,终于讲到自定义Panel了。当系统自带的几个Panel比如Gird,StackPanel,RelativePanel不能满足我们的特定要求时(其实不常见啦),自定义Panel就显得非常必要,而且因为是针对性的处理,效果也会非常好。更何况自定义Panel其实并不复杂,今天俺 ...
各位好,终于讲到自定义Panel了。当系统自带的几个Panel比如Gird,StackPanel,RelativePanel不能满足我们的特定要求时(其实不常见啦),自定义Panel就显得非常必要,而且因为是针对性的处理,效果也会非常好。更何况自定义Panel其实并不复杂,今天俺们就来学习一下。
记得上一篇自定义CommandBar在增加占位控件AppBarEmpty时,采用的是通过Page的SizeChanged事件中计算页面Width,减去CommandBar中其他控件Width后再赋值Width给AppBarEmpty的方法。就可行性而言是绝对没问题的,代码复杂度也很低,不失为一个好方法。但是复用性不太好,需要在每个Page都写上一小段代码。而我们的初衷是希望AppBarEmpty能够自动撑开,计算自身所需的Width。
遇到的困难来自StackPanel这个控件,StackPanel在计算自身所需空间时,会非常吝啬按children元素所需的最小尺寸来申请。就好比申请经费时按最下限申请,这种精神ZF部门应该学习,而公司组织TeamBuilding就应该排斥……)。说到这里,本篇的主题有了,就是通过自定义一个StackPanelEx来实现让AppBarEmpty自动撑开的效果。
前面说了,自定义Panel其实并不复杂。只有两个方法需要override:
// // Summary:(根据子元素测量控件本身需要的空间) // Provides the behavior for the Measure pass of the layout cycle. Classes can override // this method to define their own Measure pass behavior. // // Parameters: // availableSize:(控件本身的可用空间。如指定无穷大值,表示控件的大小将调整为内容的可用大小) // The available size that this object can give to child objects. Infinity can be // specified as a value to indicate that the object will size to whatever content // is available. // // Returns:(控件根据子元素大小计算得出的所需大小) // The size that this object determines it needs during layout, based on its calculations // of the allocated sizes for child objects or based on other considerations such // as a fixed container size. protected virtual Size MeasureOverride(Size availableSize); // // Summary:(根据上面测量的结果,来对子元素进行布局) // Provides the behavior for the Arrange pass of layout. Classes can override this // method to define their own Arrange pass behavior. // // Parameters: // finalSize:(控件用来排列自身及其子元素的最终确定的空间) // The final area within the parent that this object should use to arrange itself // and its children. // // Returns:(使用的实际大小) // The actual size that is used after the element is arranged in layout. protected virtual Size ArrangeOverride(Size finalSize);
原标题:UWP开发入门(五)——自定义Panel
关键词:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。