[源码下载]

背水一战 Windows 10 (72) - 控件(控件基类): UIElement - UIElement 的位置, UIElement 的布局, UIElement 的其他特性

作者:webabcd

介绍
背水一战 Windows 10 之 控件(控件基类 - UIElement)

  • 获取 UIElement 的位置
  • UIElement 的布局相关(Measure, Arrange)
  • UIElement 的非完整像素布局(UseLayoutRounding)
  • UIElement 的其他特性(Visibility, Opacity, CacheMode)

示例
1、演示如何获取 UIElement 的位置
Controls/BaseControl/UIElementDemo/TransformToVisualDemo.xaml

  1. <Page
  2. x:Class="Windows10.Controls.BaseControl.UIElementDemo.TransformToVisualDemo"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Controls.BaseControl.UIElementDemo"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11. <StackPanel Margin="10 0 10 10">
  12.  
  13. <Grid HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5">
  14. <Rectangle Name="rectangle1" Width="300" Height="200" Fill="Red" />
  15. <Rectangle Name="rectangle2" Width="150" Height="100" Fill="Green" />
  16. </Grid>
  17.  
  18. <TextBlock Name="lblMsg" Margin="5" />
  19.  
  20. </StackPanel>
  21. </Grid>
  22. </Page>

Controls/BaseControl/UIElementDemo/TransformToVisualDemo.xaml.cs

  1. /*
  2. * UIElement - UIElement(继承自 DependencyObject, 请参见 /Controls/BaseControl/DependencyObjectDemo/)
  3. * TransformToVisual(UIElement visual) - 返回相对于指定 UIElement 原点(左上角顶点)的 GeneralTransform 类型的对象,传 null 值则为相对于 app 原点(左上角顶点)
  4. *
  5. *
  6. * GeneralTransform
  7. * Point TransformPoint(Point point) - 获取相对于指定位置的位置
  8. *
  9. *
  10. * 本例用于演示如何获取 UIElement 的位置
  11. */
  12.  
  13. using System;
  14. using Windows.Foundation;
  15. using Windows.UI.Xaml;
  16. using Windows.UI.Xaml.Controls;
  17. using Windows.UI.Xaml.Media;
  18.  
  19. namespace Windows10.Controls.BaseControl.UIElementDemo
  20. {
  21. public sealed partial class TransformToVisualDemo : Page
  22. {
  23. public TransformToVisualDemo()
  24. {
  25. this.InitializeComponent();
  26.  
  27. this.Loaded += TransformToVisualDemo_Loaded;
  28. }
  29.  
  30. void TransformToVisualDemo_Loaded(object sender, RoutedEventArgs e)
  31. {
  32. Demo1();
  33. Demo2();
  34. }
  35.  
  36. // 演示如何获取 UIElement 相对于 app 原点(左上角顶点)的位置
  37. private void Demo1()
  38. {
  39. GeneralTransform generalTransform = rectangle1.TransformToVisual(null); // 获取 rectangle1 相对于 app 原点(左上角顶点)的 GeneralTransform
  40. Point point = generalTransform.TransformPoint(new Point(, )); // rectangle1 的原点(左上角顶点)相对于屏幕 0,0 点的位置
  41.  
  42. lblMsg.Text += "红色矩形的原点(左上角顶点)相对于屏幕的原点(左上角顶点)的位置:" + point.ToString();
  43. lblMsg.Text += Environment.NewLine;
  44.  
  45. }
  46.  
  47. // 演示如何获取 UIElement 相对于另一个 UIElement 原点(左上角顶点)的位置
  48. private void Demo2()
  49. {
  50. GeneralTransform generalTransform = rectangle1.TransformToVisual(rectangle2); // 获取 rectangle1 相对于 rectangle2 原点(左上角顶点)的 GeneralTransform
  51. Point point = generalTransform.TransformPoint(new Point(, )); // rectangle1 的原点(左上角顶点)相对于 rectangle2 的原点(左上角顶点)的位置
  52.  
  53. lblMsg.Text += "红色矩形的原点(左上角顶点)相对于绿色矩形的原点(左上角顶点)的位置:" + point.ToString();
  54. }
  55. }
  56. }

2、演示 UIElement 的布局相关
Controls/BaseControl/UIElementDemo/LayoutDemo.xaml

  1. <Page
  2. x:Class="Windows10.Controls.BaseControl.UIElementDemo.LayoutDemo"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Controls.BaseControl.UIElementDemo"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11. <StackPanel Margin="10 0 10 10">
  12.  
  13. <StackPanel Name="stackPanel" Margin="1 2 3 4" HorizontalAlignment="Left" VerticalAlignment="Top" Width="200" Background="Orange">
  14. <Rectangle Name="rectangle1" Fill="White" Width="100" Height="100" />
  15. </StackPanel>
  16.  
  17. <TextBlock Name="lblMsg" Margin="5" />
  18.  
  19. </StackPanel>
  20. </Grid>
  21. </Page>

Controls/BaseControl/UIElementDemo/LayoutDemo.xaml.cs

  1. /*
  2. * UIElement - UIElement(继承自 DependencyObject, 请参见 /Controls/BaseControl/DependencyObjectDemo/)
  3. * Measure(), Arrange(), InvalidateMeasure(), InvalidateArrange() - 参见 /MyControls/MyControl2.cs
  4. * DesiredSize - 获取通过 Measure() 计算后得到的期望尺寸
  5. * RenderSize - 获取通过 Arrange() 计算后得到的呈现尺寸
  6. * UpdateLayout() - 相当于依次调用 InvalidateMeasure() 和 InvalidateArrange(),然后同步等待结果,而 InvalidateMeasure() 和 InvalidateArrange() 本身是异步处理的
  7. *
  8. *
  9. * 注:
  10. * 1、uwp 的 layout 是一个递归系统,更多说明请参见 /MyControls/MyControl2.cs
  11. * 2、InvalidateMeasure() 就是递归调用自己和子辈门的 Measure()
  12. * 3、InvalidateArrange() 就是递归调用自己和子辈门的 Arrange()
  13. *
  14. *
  15. * 本例用于演示 UIElement 的布局相关
  16. */
  17.  
  18. using System;
  19. using Windows.UI.Xaml;
  20. using Windows.UI.Xaml.Controls;
  21.  
  22. namespace Windows10.Controls.BaseControl.UIElementDemo
  23. {
  24. public sealed partial class LayoutDemo : Page
  25. {
  26. public LayoutDemo()
  27. {
  28. this.InitializeComponent();
  29.  
  30. this.Loaded += LayoutDemo_Loaded;
  31. }
  32.  
  33. private void LayoutDemo_Loaded(object sender, RoutedEventArgs e)
  34. {
  35. lblMsg.Text += stackPanel.DesiredSize.ToString(); // 204,106(期望尺寸,是包括 margin 的)
  36. lblMsg.Text += Environment.NewLine;
  37. lblMsg.Text += stackPanel.RenderSize.ToString(); // 200,100(呈现尺寸,是不包括 margin 的)
  38. lblMsg.Text += Environment.NewLine;
  39.  
  40. // 更改外观
  41. stackPanel.Margin = new Thickness();
  42. rectangle1.Height = ;
  43.  
  44. // 更改外观后,布局系统会自动调用 InvalidateMeasure() 和 InvalidateArrange(),但是这是个异步的过程
  45. // 所以此处获取到的 DesiredSize 和 RenderSize 仍然是更改外观之前的值
  46. lblMsg.Text += stackPanel.DesiredSize.ToString(); // 204,106(期望尺寸,是包括 margin 的)
  47. lblMsg.Text += Environment.NewLine;
  48. lblMsg.Text += stackPanel.RenderSize.ToString(); // 200,100(呈现尺寸,是不包括 margin 的)
  49. lblMsg.Text += Environment.NewLine;
  50.  
  51. // 如果想要同步知道结果的话就调用 UpdateLayout()
  52. stackPanel.UpdateLayout();
  53.  
  54. // 所以此处获取到的 DesiredSize 和 RenderSize 为更改外观之后的值
  55. lblMsg.Text += stackPanel.DesiredSize.ToString(); // 210,310(期望尺寸,是包括 margin 的)
  56. lblMsg.Text += Environment.NewLine;
  57. lblMsg.Text += stackPanel.RenderSize.ToString(); // 200,300(呈现尺寸,是不包括 margin 的)
  58. }
  59. }
  60. }

3、演示 UIElement 的 UseLayoutRounding 的应用
Controls/BaseControl/UIElementDemo/UseLayoutRoundingDemo.xaml

  1. <Page
  2. x:Class="Windows10.Controls.BaseControl.UIElementDemo.UseLayoutRoundingDemo"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Controls.BaseControl.UIElementDemo"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11. <StackPanel Margin="10 0 10 10">
  12.  
  13. <!--
  14. UseLayoutRounding - 是否使用完整像素布局。默认值为 true
  15. 举例说明:如果设置了某个元素的 Margin="1.6"(非完整像素),那么
  16. 1、当 UseLayoutRounding="true" 时,则设置为 Margin="1.6" 的元素的各个方向的 Margin 是不相等的,但是显示清晰
  17. 2、当 UseLayoutRounding="false" 时,则设置为 Margin="1.6" 的元素的各个方向的 Margin 是相等的,但是显示不清晰
  18.  
  19. 注:UseLayoutRounding 是会自动继承的,即子元素会继承父元素的 UseLayoutRounding 设置
  20. -->
  21. <Grid Width="200" Height="200" Margin="5" HorizontalAlignment="Left">
  22. <Border BorderBrush="Black" Background="White" BorderThickness="1"></Border>
  23. <Border BorderBrush="Black" Background="White" BorderThickness="1" Margin="1.6" UseLayoutRounding="True"></Border>
  24. </Grid>
  25.  
  26. <Grid Width="200" Height="200" Margin="5" HorizontalAlignment="Left">
  27. <Border BorderBrush="Black" Background="White" BorderThickness="1"></Border>
  28. <Border BorderBrush="Black" Background="White" BorderThickness="1" Margin="1.6" UseLayoutRounding="False"></Border>
  29. </Grid>
  30.  
  31. </StackPanel>
  32. </Grid>
  33. </Page>

Controls/BaseControl/UIElementDemo/UseLayoutRoundingDemo.xaml.cs

  1. /*
  2. * UIElement - UIElement(继承自 DependencyObject, 请参见 /Controls/BaseControl/DependencyObjectDemo/)
  3. * UseLayoutRounding - 是否使用完整像素布局。默认值为 true
  4. *
  5. *
  6. * 本例用于演示 UIElement 的 UseLayoutRounding 的应用
  7. */
  8.  
  9. using Windows.UI.Xaml.Controls;
  10.  
  11. namespace Windows10.Controls.BaseControl.UIElementDemo
  12. {
  13. public sealed partial class UseLayoutRoundingDemo : Page
  14. {
  15. public UseLayoutRoundingDemo()
  16. {
  17. this.InitializeComponent();
  18. }
  19. }
  20. }

4、演示 UIElement 的其他特性
Controls/BaseControl/UIElementDemo/OthersDemo.xaml

  1. <Page
  2. x:Class="Windows10.Controls.BaseControl.UIElementDemo.OthersDemo"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Controls.BaseControl.UIElementDemo"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11. <StackPanel Margin="5">
  12.  
  13. <Rectangle Fill="Orange" Width="100" Height="100" Margin="5" Visibility="Collapsed" />
  14. <Rectangle Fill="Orange" Width="100" Height="100" Margin="5" Opacity="0.5" CacheMode="BitmapCache" />
  15.  
  16. </StackPanel>
  17. </Grid>
  18. </Page>

Controls/BaseControl/UIElementDemo/OthersDemo.xaml.cs

  1. /*
  2. * UIElement - UIElement(继承自 DependencyObject, 请参见 /Controls/BaseControl/DependencyObjectDemo/)
  3. * Visibility - 可见性
  4. * Visible - 显示
  5. * Collapsed - 不显示,且不占位
  6. * Opacity - 不透明度(0.0 - 1.0 之间,默认值为 1.0)
  7. * CacheMode - 缓存模式
  8. * null - 默认值
  9. * BitmapCache - 用 GPU 渲染 RenderTransform 和 Opacity
  10. * 如果 RenderTransform 或 Opacity 使用了 storyboard 动画的话,动画一律将变为 Dependent Animation 而不是 Independent Animation,这样性能就会变差。一般来说不用设置成 BitmapCache 模式
  11. *
  12. *
  13. * 本例用于演示 UIElement 的其他特性
  14. */
  15.  
  16. using Windows.UI.Xaml.Controls;
  17.  
  18. namespace Windows10.Controls.BaseControl.UIElementDemo
  19. {
  20. public sealed partial class OthersDemo : Page
  21. {
  22. public OthersDemo()
  23. {
  24. this.InitializeComponent();
  25.  
  26. // this.Visibility = Visibility.Collapsed;
  27. // this.Opacity = 0.5;
  28. // this.CacheMode = null;
  29. // this.CacheMode = new BitmapCache();
  30. }
  31. }
  32. }

OK
[源码下载]

背水一战 Windows 10 (72) - 控件(控件基类): UIElement - UIElement 的位置, UIElement 的布局, UIElement 的其他特性的更多相关文章

  1. 背水一战 Windows 10 (16) - 动画: ThemeAnimation(主题动画)

    [源码下载] 背水一战 Windows 10 (16) - 动画: ThemeAnimation(主题动画) 作者:webabcd 介绍背水一战 Windows 10 之 动画 PopInThemeA ...

  2. 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog

    [源码下载] 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog 作者:webabcd 介绍背水一战 Windows 10 之 控 ...

  3. 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu

    [源码下载] 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu 作者:webabcd 介绍背水一战 Windows 10 之 控件(弹 ...

  4. 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout

    [源码下载] 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout 作者:webabcd 介绍背水一战 Windows 10 之 ...

  5. 背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing

    [源码下载] 背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing 作者:webabcd 介绍背水一 ...

  6. 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch

    [源码下载] 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch 作者:webabcd 介绍背水一 ...

  7. 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox

    [源码下载] 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(选择类) Sel ...

  8. 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButton, AppBarButton, AppBarToggleButton

    [源码下载] 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButt ...

  9. 背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox

    [源码下载] 背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(文本类) AutoSug ...

随机推荐

  1. java中Class.getMethod方法

    Method Class.getMethod(String name, Class<?>... parameterTypes)的作用是获得对象所声明的公开方法 该方法的第一个参数name是 ...

  2. SpringMvc 使用Thumbnails压缩图片

    ```java @PostMapping(value = "/upLoadFile") @ApiOperation(value = "上传文件") public ...

  3. unity编程心得

    1.  不要通过public变量 从工程面板 直接 拖 GameObjct 的引用, 当这样的public变量很多 ,子物体很多,又没有做成预制体,,别人重新移植这段功能会很麻烦,,应该用GameOb ...

  4. oracle数据库分页总结

    /* BEGIN CREATE TABLE APPUSER(IDS NUMBER(8), USERNAME VARCHAR2(20), PASSWORD VARCHAR2(20), CTIME DAT ...

  5. 【VBA】セールの最初起動時、VBAを自動的に実行方法

    方法一 セールの「ThisWorkbook」にて.「Workbook」の「Open」関数にて.ロジックを追加する Private Sub Workbook_Open() Msgbox "He ...

  6. HDU 5977 Garden of Eden(点分治求点对路径颜色数为K)

    Problem Description When God made the first man, he put him on a beautiful garden, the Garden of Ede ...

  7. 763. Partition Labels 相同字母出现在同一块中,且块数最多

    [抄题]: A string S of lowercase letters is given. We want to partition this string into as many parts ...

  8. 解决:AttributeError: module 'requests' has no attribute 'post'”

    今天学习Requests库,当用pip install requests安装后,写了一段代码报错:AttributeError: module ‘requests‘ has no attribute ...

  9. Centos7 进入单用户模式,修复系统

    一.开机时进入如下界面,(按下方向键盘,阻止系统自动继续) 按e键出现下面界面 按方向键下,定位到最后,找到“ro”一行,ro的意思是read only,将“ro”替换成 rw init=/sysro ...

  10. React-router4 第五篇 Preventing Transitions 防止转换

    文档地址:https://reacttraining.com/react-router/web/example/preventing-transitions 大概意思就是说:我在这个页面上写东西呢?不 ...