[源码下载]

背水一战 Windows 10 (75) - 控件(控件基类): FrameworkElement - 基础知识, 相关事件, HorizontalAlignment, VerticalAlignment

作者:webabcd

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

  • 基础知识
  • 相关事件
  • HorizontalAlignment 和 VerticalAlignment

示例
1、演示 FrameworkElement 的基础知识
Controls/BaseControl/FrameworkElementDemo/Demo1.xaml

<Page
x:Class="Windows10.Controls.BaseControl.FrameworkElementDemo.Demo1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.BaseControl.FrameworkElementDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Orange">
<StackPanel Name="stackPanel" Background="Blue" Margin="100"> <TextBlock Name="lblMsg" Margin="5" /> </StackPanel>
</Grid>
</Page>

Controls/BaseControl/FrameworkElementDemo/Demo1.xaml.cs

/*
* FrameworkElement - FrameworkElement(继承自 UIElement, 请参见 /Controls/BaseControl/UIElementDemo/)
* MinWidth, MinHeight - 最小宽高,默认值为 0
* MaxWidth, MaxHeight - 最大宽高,默认值为 double.PositiveInfinity
* Width, Height - 宽高,默认值为 NaN
* ActualWidth, ActualHeight - 实际宽高,来自 UIElement 的 RenderSize 属性(uwp 的 layout 是一个递归系统,更多说明请参见 /MyControls/MyControl2.cs)
* Margin - Margin
* 它是一个 Thickness 类型的对象,在 C# 端就构造这个对象即可
* 在 xaml 端设置的话,其规则为“左,上,右,下”或“左右,上下”或“左上右下”,可以用逗号分隔也可以用空格分隔
* Name - 名字
* FindName() - 查找当前页面的指定名字的对象
* Parent - 获取当前对象的父对象
* Tag - 用于保存任意对象
* Language - 设置或获取当前元素及其所有子元素的语言信息(没什么实际效果,就是一个标记而已)
* BaseUri - 获取当前对象所在的 xaml 页面的 uri 地址
* DataContext - 数据上下文(参见 /Bind/DataContextChanged.xaml)
* RequestedTheme - 主题(参见 /Resource/ThemeResourceDemo.xaml.cs)
* Resources - 资源字典(参见 /Resource/ResourceDictionaryDemo.xaml)
* Style - 样式(参见 /Controls/UI/Style.xaml)
* GetBindingExpression() - 获取指定属性的绑定信息(参见 /Bind/UpdateSourceTrigger.xaml.cs)
* SetBinding() - 设置绑定信息(参见 /Bind/BindingElement.xaml.cs)
*
*
* 本例用于演示 FrameworkElement 的基础知识
*/ using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace Windows10.Controls.BaseControl.FrameworkElementDemo
{
public sealed partial class Demo1 : Page
{
public Demo1()
{
this.InitializeComponent(); this.Loaded += Demo1_Loaded;
} private void Demo1_Loaded(object sender, RoutedEventArgs e)
{
stackPanel.MinWidth = ;
stackPanel.MinHeight = ;
stackPanel.MaxWidth = double.PositiveInfinity;
stackPanel.MaxHeight = double.PositiveInfinity; lblMsg.Text += $"stackPanel.ActualWidth:{stackPanel.ActualWidth}, stackPanel.ActualHeight:{stackPanel.ActualHeight}";
lblMsg.Text += Environment.NewLine; if (this.FindName("lblMsg") as TextBlock == lblMsg)
{
lblMsg.Text += "this.FindName(\"lblMsg\") as TextBlock == lblMsg";
lblMsg.Text += Environment.NewLine;
} if (lblMsg.Parent as StackPanel == stackPanel)
{
lblMsg.Text += "lblMsg.Parent as StackPanel == stackPanel";
lblMsg.Text += Environment.NewLine;
} lblMsg.Text += "BaseUri:" + this.BaseUri;
lblMsg.Text += Environment.NewLine; lblMsg.Tag = "i am webabcd";
lblMsg.Text += "lblMsg.Tag:" + lblMsg.Tag;
lblMsg.Text += Environment.NewLine;
}
}
}

2、演示 FrameworkElement 的相关事件
Controls/BaseControl/FrameworkElementDemo/Demo2.xaml

<Page
x:Class="Windows10.Controls.BaseControl.FrameworkElementDemo.Demo2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.BaseControl.FrameworkElementDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Orange">
<StackPanel Name="stackPanel" Background="Blue" Margin="100"> <TextBlock Name="lblMsg" Margin="5" /> </StackPanel>
</Grid>
</Page>

Controls/BaseControl/FrameworkElementDemo/Demo2.xaml.cs

/*
* FrameworkElement - FrameworkElement(继承自 UIElement, 请参见 /Controls/BaseControl/UIElementDemo/)
* DataContextChanged - 数据上下文发生改变后触发的事件(参见 /Bind/DataContextChanged.xaml)
* Loading - 在 OnNavigatedTo 之后,由外到内触发 Loading 事件
* Loaded - 由内到外触发 Loaded 事件
* Unloaded - 在 OnNavigatedFrom 之后,由外到内触发 Unloaded 事件
* SizeChanged - 在尺寸(ActualWidth 或 ActualHeight)发生变化时触发的事件,位置等发生变化时不会触发
* LayoutUpdated - 布局更新时触发的事件,比如尺寸发生了变化或位置发生了变化等
*
*
* 本例用于演示 FrameworkElement 的相关事件
*/ using System;
using System.Diagnostics;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation; namespace Windows10.Controls.BaseControl.FrameworkElementDemo
{
public sealed partial class Demo2 : Page
{
public Demo2()
{
this.InitializeComponent(); this.Loading += page_Loading;
this.Loaded += page_Loaded;
this.Unloaded += page_Unloaded; stackPanel.Loading += stackPanel_Loading;
stackPanel.Loaded += stackPanel_Loaded;
stackPanel.Unloaded += stackPanel_Unloaded; lblMsg.Loading += lblMsg_Loading;
lblMsg.Loaded += lblMsg_Loaded;
lblMsg.Unloaded += lblMsg_Unloaded; lblMsg.SizeChanged += lblMsg_SizeChanged;
lblMsg.LayoutUpdated += lblMsg_LayoutUpdated;
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
lblMsg.Text += "OnNavigatedTo";
lblMsg.Text += Environment.NewLine;
}
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
Debug.WriteLine("OnNavigatingFrom");
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
Debug.WriteLine("OnNavigatedFrom");
} private void page_Loading(FrameworkElement sender, object args)
{
lblMsg.Text += "page_Loading";
lblMsg.Text += Environment.NewLine;
}
private void page_Loaded(object sender, RoutedEventArgs e)
{
lblMsg.Text += "page_Loaded";
lblMsg.Text += Environment.NewLine;
}
private void page_Unloaded(object sender, RoutedEventArgs e)
{
Debug.WriteLine("page_Unloaded");
} private void stackPanel_Loading(FrameworkElement sender, object args)
{
lblMsg.Text += "stackPanel_Loading";
lblMsg.Text += Environment.NewLine;
}
private void stackPanel_Loaded(object sender, RoutedEventArgs e)
{
lblMsg.Text += "stackPanel_Loaded";
lblMsg.Text += Environment.NewLine;
}
private void stackPanel_Unloaded(object sender, RoutedEventArgs e)
{
Debug.WriteLine("stackPanel_Unloaded");
} private void lblMsg_Loading(FrameworkElement sender, object args)
{
lblMsg.Text += "lblMsg_Loading";
lblMsg.Text += Environment.NewLine;
}
private void lblMsg_Loaded(object sender, RoutedEventArgs e)
{
lblMsg.Text += "lblMsg_Loaded";
lblMsg.Text += Environment.NewLine;
}
private void lblMsg_Unloaded(object sender, RoutedEventArgs e)
{
Debug.WriteLine("lblMsg_Unloaded");
} private void lblMsg_SizeChanged(object sender, SizeChangedEventArgs e)
{
Debug.WriteLine($"lblMsg_SizeChanged, PreviousSize:{e.PreviousSize}, NewSize:{e.NewSize}"); // 注:如果在这里又改变了 lblMsg 的尺寸的话,则会发生循环调用,编译时会出现“Layout cycle detected. Layout could not complete.”错误
}
private void lblMsg_LayoutUpdated(object sender, object e)
{
Debug.WriteLine("lblMsg_LayoutUpdated"); // 注:如果在这里又改变了 lblMsg 的布局的话,则会发生循环调用,编译时会出现“Layout cycle detected. Layout could not complete.”错误
}
}
}

3、演示 FrameworkElement 的 HorizontalAlignment 和 VerticalAlignment 的应用
Controls/BaseControl/FrameworkElementDemo/Demo3.xaml

<Page
x:Class="Windows10.Controls.BaseControl.FrameworkElementDemo.Demo3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.BaseControl.FrameworkElementDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Orange">
<StackPanel Name="stackPanel" Background="Blue" Margin="100" VerticalAlignment="Stretch"> <ComboBox Name="cmbHorizontalAlignment" PlaceholderText="HorizontalAlignment" SelectionChanged="cmbHorizontalAlignment_SelectionChanged" Margin="5">
<ComboBoxItem>Left</ComboBoxItem>
<ComboBoxItem>Center</ComboBoxItem>
<ComboBoxItem>Right</ComboBoxItem>
<ComboBoxItem IsSelected="True">Stretch</ComboBoxItem>
</ComboBox> <ComboBox Name="cmbVerticalAlignment" PlaceholderText="VerticalAlignment" SelectionChanged="cmbVerticalAlignment_SelectionChanged" Margin="5">
<ComboBoxItem>Top</ComboBoxItem>
<ComboBoxItem>Center</ComboBoxItem>
<ComboBoxItem>Bottom</ComboBoxItem>
<ComboBoxItem IsSelected="True">Stretch</ComboBoxItem>
</ComboBox> </StackPanel>
</Grid>
</Page>

Controls/BaseControl/FrameworkElementDemo/Demo3.xaml.cs

/*
* FrameworkElement - FrameworkElement(继承自 UIElement, 请参见 /Controls/BaseControl/UIElementDemo/)
* HorizontalAlignment - 水平对齐方式(Left, Center, Right, Stretch)
* VerticalAlignment - 垂直对齐方式(Top, Center, Bottom, Stretch)
*
*
* 本例用于演示 FrameworkElement 的 HorizontalAlignment 和 VerticalAlignment 的应用
*/ using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace Windows10.Controls.BaseControl.FrameworkElementDemo
{
public sealed partial class Demo3 : Page
{
public Demo3()
{
this.InitializeComponent();
} private void cmbHorizontalAlignment_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
stackPanel.HorizontalAlignment = (HorizontalAlignment)Enum.Parse(typeof(HorizontalAlignment), (e.AddedItems[] as ComboBoxItem).Content.ToString());
} private void cmbVerticalAlignment_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
stackPanel.VerticalAlignment = (VerticalAlignment)Enum.Parse(typeof(VerticalAlignment), (e.AddedItems[] as ComboBoxItem).Content.ToString());
} }
}

OK
[源码下载]

背水一战 Windows 10 (75) - 控件(控件基类): FrameworkElement - 基础知识, 相关事件, HorizontalAlignment, VerticalAlignment的更多相关文章

  1. 背水一战 Windows 10 (68) - 控件(控件基类): UIElement - Pointer 相关事件, Tap 相关事件, Key 相关事件, Focus 相关事件

    [源码下载] 背水一战 Windows 10 (68) - 控件(控件基类): UIElement - Pointer 相关事件, Tap 相关事件, Key 相关事件, Focus 相关事件 作者: ...

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

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

  3. 背水一战 Windows 10 (38) - 控件(布局类): Panel, Canvas, RelativePanel, StackPanel, Grid

    [源码下载] 背水一战 Windows 10 (38) - 控件(布局类): Panel, Canvas, RelativePanel, StackPanel, Grid 作者:webabcd 介绍背 ...

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. css学习2

    1.垂直居中 -父元素高度确定的单行文本: 设置父元素的 height 和 line-height  高度一致来实现的.(height: 该元素的高度:line-height: 行高(行间距),指在文 ...

  2. Linux Shell 简介

    什么是 Shell Shell 是用户和 Linux 内核之间的接口程序,当从 Shell 或其他程序向 Linux 传递命令时,内核会做出相应的反应: Shell 是一个命令语言解释器,它拥有自己内 ...

  3. git command line 提交代码

    echo "# spring-boot-apollo-demo" >> README.md git init git add README.md git commit ...

  4. cdnbest独立主控配置自定义错误页面

    注:自定义错误的域名最好配置ssl,不配碰到有使用ssl的域名,错误码显示就会有问题 (1)登陆管理后台点区域列表-->配置-->错误url 解析一个域名到你的cdn主控,然后输入这个ur ...

  5. cobbler的ks文件带有特殊字符导致错误

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAykAAACWAQMAAADg0FUUAAAAAXNSR0IArs4c6QAAAAZQTFRFq6urAA

  6. netty(六) websocket开发应用

    package com.lance.net.server.common; import java.net.InetSocketAddress; import org.springframework.s ...

  7. 100道c++面试题(上)

    1. new, delete, malloc, free关系 new/delete是c++的运算符,delete会调用对象的析构函数: malloc/free是c/c++的标准库函数,free只释放内 ...

  8. vue去掉地址栏#(带来的后果)

    按以下修改后,带来的后果:打包后部分图片访问不到,首页没加载router-view内容 router 的index中配置模式设置为history export default new Router({ ...

  9. 6E - 寒冰王座

    不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票(记住,只有一张钞票),为了防止自己在战斗中频繁的死掉,他决定给自己买一些道具,于是他来到了地精商店前. 死亡骑士:"我要买道具!" ...

  10. hbase-基础架构

    介绍 hbase架构主要由hmaster,zookeeper和regionserver三部分构成,底层数据存储在hdfs中 hmaster 允许有多个master节点,使用zookeeper控制,保证 ...