博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
背水一战 Windows 10 (79) - 自定义控件: Layout 系统, 控件模板, 事件处理
阅读量:7178 次
发布时间:2019-06-29

本文共 6392 字,大约阅读时间需要 21 分钟。

原文:

背水一战 Windows 10 (79) - 自定义控件: Layout 系统, 控件模板, 事件处理

作者:
介绍
背水一战 Windows 10 之 控件(自定义控件)

  • 自定义控件的 Layout 系统
  • 自定义控件的控件模板和事件处理的相关知识点

示例
1、演示自定义控件的 Layout 系统
/MyControls/MyControl2.cs

/* * 本例通过一个自定义控件来演示 uwp 中可视元素的 Layout 系统 *  * uwp 的 layout 是一个递归系统,本 demo 就递归的一个过程做说明(步骤顺序参见代码注释中的序号) *  *  * Measure() 的作用是测量尺寸 * Arrange() 的作用是排列元素 */using Windows.UI.Xaml.Controls;using Windows.UI.Xaml;using Windows.Foundation;using System;using System.Linq;using System.Diagnostics;using System.Collections.Generic;namespace MyControls{    ///     /// 一个每行都会自动缩进的 Panel    ///     public sealed class MyControl2 : Panel    {        // 相对上一行的缩进值        const double INDENT = 20;        public MyControl2()        {        }        // 1、首先爸爸知道自己能够提供的尺寸 availableSize,然后告诉儿子们        protected override Size MeasureOverride(Size availableSize) // 测量出期待的尺寸并返回        {            // 2、儿子们收到 availableSize 后,又结合了自身的实际情况,然后告诉爸爸儿子们所期望的尺寸 desiredSize            List
widthList = new List
(); Size desiredSize = new Size(0, 0); foreach (UIElement child in this.Children) { // 如果 child 是 FrameworkElement 的话,则当调用其 Measure() 方法时会自动调用其 MeasureOverride() 方法 child.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)); widthList.Add(child.DesiredSize.Width); desiredSize.Height += child.DesiredSize.Height; } if (this.Children.Count > 0) { desiredSize.Width = widthList.Max(); desiredSize.Width += INDENT * (this.Children.Count - 1); } Debug.WriteLine("availableSize: " + availableSize.ToString()); Debug.WriteLine("desiredSize: " + desiredSize.ToString()); return desiredSize; } // 3、爸爸收到儿子们的反馈后,告诉儿子们自己最终提供的尺寸 finalSize protected override Size ArrangeOverride(Size finalSize) // 排列元素,并返回呈现尺寸 { // 4、儿子们根据 finalSize 安排各自的位置,然后爸爸的呈现尺寸也就确定了 renderSize Point childPosition = new Point(0, 0); foreach (UIElement child in this.Children) { // 如果 child 是 FrameworkElement 的话,则当调用其 Arrange() 方法时会自动调用其 ArrangeOverride() 方法 child.Arrange(new Rect(childPosition, new Size(child.DesiredSize.Width, child.DesiredSize.Height))); childPosition.X += INDENT; childPosition.Y += child.DesiredSize.Height; } Size renderSize = new Size(0, 0); renderSize.Width = finalSize.Width; renderSize.Height = childPosition.Y; Debug.WriteLine("finalSize: " + finalSize.ToString()); Debug.WriteLine("renderSize: " + renderSize.ToString()); return finalSize; } }}/* * 输出结果如下(运行 /Controls/CustomControl/Demo2.xaml 示例) * availableSize: 800,Double.PositiveInfinity * desiredSize: 141,120 * finalSize: 800,120 * renderSize: 800,120*//* * 注: * UIElement * 调用 Measure() 方法后会更新 DesiredSize 属性 * 调用 Arrange() 方法后会更新 RenderSize 属性 * UpdateLayout() - 强制 layout 递归更新 * * FrameworkElement - 继承自 UIElement * MeasureOverride() - 在 Measure() 中自动调用 * ArrangeOverride() - 在 Arrange() 中自动调用 * ActualWidth 和 ActualHeight 来自 RenderSize,每次 UpdateLayout() 后都会被更新 *//** 注:* 1、uwp 的 layout 是一个递归系统* 2、UIElement 的 InvalidateMeasure() 就是递归调用自己和子辈门的 Measure()* 3、UIElement 的 InvalidateArrange() 就是递归调用自己和子辈门的 Arrange()* * 一个通过 uwp 自带控件说明 layout 的示例,请参见:/Controls/BaseControl/UIElementDemo/LayoutDemo.xaml.cs*/

Controls/CustomControl/Demo2.xaml

Controls/CustomControl/Demo2.xaml.cs

/* * 本例用于演示元素的 Layout 系统 */using Windows.UI.Xaml.Controls;namespace Windows10.Controls.CustomControl{    public sealed partial class Demo2 : Page    {        public Demo2()        {            this.InitializeComponent();        }    }}

2、演示自定义控件的控件模板和事件处理的相关知识点
/MyControls/themes/MyControl3.xaml

/MyControls/MyControl3.cs

/* * 开发一个自定义控件,用于演示控件模板和事件处理的相关知识点 */using Windows.UI.Xaml.Controls;using Windows.UI.Xaml;using Windows.UI.Xaml.Media;using Windows.UI.Xaml.Input;namespace MyControls{    ///     /// 自定义控件    ///     public sealed class MyControl3 : Control    {        public MyControl3()        {            this.DefaultStyleKey = typeof(MyControl3);        }        // ApplyTemplate() - 强制加载控件模板,一般不用调用(因为控件模板会自动加载)。有一种使用场景是:当父控件应用控件模板时要求子控件必须先应用控件模板以便父控件使用时,则可以先调用子控件的此方法        // GetTemplateChild() - 查找控件模板中的指定名字的元素        // override OnApplyTemplate() - 应用控件模板时调用        protected override void OnApplyTemplate()        {            base.OnApplyTemplate();            TextBlock textBlock = (TextBlock)GetTemplateChild("textBlock");            if (this.Background is SolidColorBrush)            {                textBlock.Text = $"background: {((SolidColorBrush)this.Background).Color}";            }            VisualStateManager.GoToState(this, "Normal", false);        }        // override GoToElementStateCore() - VisualState 转换时调用(此方法仅在自定义 ContentPresenter 并将其应用于 GridView 或 ListView 的 ItemContainerStyle 时才会被调用)        //     参见:/Controls/CollectionControl/ItemsControlDemo/MyItemPresenter.cs        protected override bool GoToElementStateCore(string stateName, bool useTransitions)        {            return base.GoToElementStateCore(stateName, useTransitions);        }        // 在 Control 中有很多可 override 的事件处理方法,详见文档        protected override void OnPointerEntered(PointerRoutedEventArgs e)        {            VisualStateManager.GoToState(this, "PointerOver", true);        }        protected override void OnPointerExited(PointerRoutedEventArgs e)        {            VisualStateManager.GoToState(this, "Normal", false);        }    }}

Controls/CustomControl/Demo3.xaml

Controls/CustomControl/Demo3.xaml.cs

/* * 本例用于演示自定义控件的控件模板和事件处理的相关知识点 */using Windows.UI.Xaml.Controls;namespace Windows10.Controls.CustomControl{    public sealed partial class Demo3 : Page    {        public Demo3()        {            this.InitializeComponent();        }    }}

OK

转载地址:http://bfszm.baihongyu.com/

你可能感兴趣的文章
触发Full GC执行的情况
查看>>
docker 自制alpine-lnp镜像
查看>>
C# 当前目录你了解多少
查看>>
国内外知名IT科技博客
查看>>
你不知道的JavaScript(五)内置对象模版
查看>>
Java阶段性总结与获奖感想
查看>>
jQuery(1)
查看>>
iphone 一个强大的动画效果,真的很好,很强大哦
查看>>
第七周(2)
查看>>
Tui-x 命名规则( 转 ) ----- 3
查看>>
SQL Server修改表结构后批量更新所有视图
查看>>
目前流行的源程序版本管理软件和项目管理软件都有哪些,各有什么优缺点?
查看>>
canvas-6font.html
查看>>
jdk9 新特征(译)--3
查看>>
Python 简介
查看>>
Spring AOP
查看>>
第一次作业
查看>>
Python基础5:列表 元祖 字典 集合 Json
查看>>
poj 2109Power of Cryptography 解题报告
查看>>
CSS基础(六):浮动深入
查看>>