[源码下载]

背水一战 Windows 10 (26) - XAML: x:DeferLoadStrategy, x:Null

作者:webabcd

介绍
背水一战 Windows 10 之 XAML

  • x:DeferLoadStrategy="Lazy" - 用于指定一个 UIElement 为一个延迟加载元素
  • x:Null - null

示例
1、x:DeferLoadStrategy 通过 FindName 加载
Xaml/DeferLoadStrategy/Demo1.xaml

<Page
x:Class="Windows10.Xaml.DeferLoadStrategy.Demo1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Xaml.DeferLoadStrategy"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <TextBlock Name="lblMsg" TextWrapping="Wrap" Margin="5" /> <!--
x:DeferLoadStrategy="Lazy" - 其仅作用于 UIElement,可以指定一个 UIElement 为一个延迟加载元素
1、标记为延迟加载的元素必须要指定其 x:Name
2、UIElement 不是在任何情况下都能标记为 x:DeferLoadStrategy="Lazy" 的,不是只有 FindName 才能加载延迟加载元素,具体参见文档:https://msdn.microsoft.com/en-us/windows/uwp/xaml-platform/x-deferloadstrategy-attribute
3、将 UIElement 标记为 x:DeferLoadStrategy="Lazy" 的好处是可以减少页面的启动时间,带来的问题是会增加内存的使用量,每个延迟加载元素大约多耗费 600 字节左右的内存
4、延迟加载元素在加载后,会触发其 Loading 事件
-->
<TextBlock Name="textBlock" Margin="5" x:DeferLoadStrategy="Lazy" /> </StackPanel>
</Grid>
</Page>

Xaml/DeferLoadStrategy/Demo1.xaml.cs

/*
* 演示 x:DeferLoadStrategy 的相关知识点
*
* 本例演示通过“FindName”加载延迟加载元素
*/ using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace Windows10.Xaml.DeferLoadStrategy
{
public sealed partial class Demo1 : Page
{
public Demo1()
{
this.InitializeComponent(); this.Loaded += DeferLoadStrategyDemo_Loaded;
} private void DeferLoadStrategyDemo_Loaded(object sender, RoutedEventArgs e)
{
try
{
// 抛出异常
textBlock.Text = "我是 TextBlock";
}
catch (Exception ex)
{
lblMsg.Text += ex.ToString();
lblMsg.Text += Environment.NewLine;
} // 可以通过 FindName() 来加载 x:DeferLoadStrategy="Lazy" 元素
this.FindName(nameof(textBlock)); textBlock.Text = "我是 TextBlock";
}
}
}

2、x:DeferLoadStrategy 通过绑定加载
Xaml/DeferLoadStrategy/Demo2.xaml

<Page
x:Class="Windows10.Xaml.DeferLoadStrategy.Demo2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Xaml.DeferLoadStrategy"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <TextBox Name="textBox1" Margin="5" /> <!--延迟加载元素-->
<TextBox Name="textBox2" x:DeferLoadStrategy="Lazy" Text="我是 TextBox" Margin="5" /> <!--将 textBox2 与 textBox1 绑定后,textBox2 就会被加载-->
<Button Name="button" Content="将 textBox1 的 Text 绑定到 textBox2 的 Text" Click="button_Click" Margin="5" /> </StackPanel>
</Grid>
</Page>

Xaml/DeferLoadStrategy/Demo2.xaml.cs

/*
* 演示 x:DeferLoadStrategy 的相关知识点
*
* 本例演示通过“绑定”加载延迟加载元素
*/ using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data; namespace Windows10.Xaml.DeferLoadStrategy
{
public sealed partial class Demo2 : Page
{
public Demo2()
{
this.InitializeComponent();
} private void button_Click(object sender, RoutedEventArgs e)
{
// 实例化 Binding 对象
Binding binding = new Binding()
{
ElementName = nameof(textBox2), // textBox2 是延迟加载元素,将其与 textBox1 绑定后,textBox2 就会被加载
Path = new PropertyPath(nameof(TextBox.Text)),
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
}; // 将目标对象的目标属性与指定的 Binding 对象关联
BindingOperations.SetBinding(textBox1, TextBox.TextProperty, binding);
}
}
}

3、x:DeferLoadStrategy 通过 Storyboard 加载
Xaml/DeferLoadStrategy/Demo3.xaml

<Page
x:Class="Windows10.Xaml.DeferLoadStrategy.Demo3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Xaml.DeferLoadStrategy"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Page.Resources>
<Storyboard x:Name="sb">
<ColorAnimation Storyboard.TargetName="ellipse"
Storyboard.TargetProperty="(Ellipse.Fill).(SolidColorBrush.Color)"
BeginTime="00:00:00"
From="Orange"
To="Blue"
Duration="0:0:3"
AutoReverse="true"
RepeatBehavior="Forever ">
</ColorAnimation>
</Storyboard>
</Page.Resources> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <!--延迟加载元素-->
<Ellipse x:Name="ellipse" Fill="Orange" Width="200" Height="100" x:DeferLoadStrategy="Lazy" HorizontalAlignment="Left" Margin="5" /> <!--启动一个引用了延迟加载元素的动画后,该延迟加载元素就会被加载-->
<Button Name="button" Content="开始动画" Click="button_Click" Margin="5" /> </StackPanel>
</Grid>
</Page>

Xaml/DeferLoadStrategy/Demo3.xaml.cs

/*
* 演示 x:DeferLoadStrategy 的相关知识点
*
* 本例演示通过“Storyboard”加载延迟加载元素
*/ using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace Windows10.Xaml.DeferLoadStrategy
{
public sealed partial class Demo3 : Page
{
public Demo3()
{
this.InitializeComponent();
} private void button_Click(object sender, RoutedEventArgs e)
{
// 启动一个引用了延迟加载元素的动画后,该延迟加载元素就会被加载
sb.Begin();
}
}
}

4、x:DeferLoadStrategy 通过 Setter 加载
Xaml/DeferLoadStrategy/Demo4.xaml

<Page
x:Class="Windows10.Xaml.DeferLoadStrategy.Demo4"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Xaml.DeferLoadStrategy"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent"> <VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="WindowSizeStates">
<VisualState>
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="360" />
</VisualState.StateTriggers>
</VisualState>
<VisualState>
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="0" />
</VisualState.StateTriggers>
<VisualState.Setters>
<!--窗口宽度小于 360 时调用,此处 setter 引用了延迟加载元素,该延迟加载元素将会被加载-->
<Setter Target="textBox.Foreground" Value="Red" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup> </VisualStateManager.VisualStateGroups> <StackPanel Margin="10 0 10 10"> <!--延迟加载元素-->
<TextBox Name="textBox" Text="我是 TextBox" x:DeferLoadStrategy="Lazy" /> </StackPanel>
</Grid>
</Page>

Xaml/DeferLoadStrategy/Demo4.xaml.cs

/*
* 演示 x:DeferLoadStrategy 的相关知识点
*
* 本例演示通过“Setter”加载延迟加载元素
*/ using Windows.UI.Xaml.Controls; namespace Windows10.Xaml.DeferLoadStrategy
{
public sealed partial class Demo4 : Page
{
public Demo4()
{
this.InitializeComponent();
}
}
}

5、x:DeferLoadStrategy 通过 GetTemplateChild 加载
Xaml/DeferLoadStrategy/Demo5.xaml

<Page
x:Class="Windows10.Xaml.DeferLoadStrategy.Demo5"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Xaml.DeferLoadStrategy"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Page.Resources>
<Style TargetType="local:TitledImage">
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:TitledImage">
<Grid>
<Image Source="{TemplateBinding Source}" Width="200" Height="100" Stretch="Uniform" HorizontalAlignment="Center" VerticalAlignment="Center" />
<!--延迟加载元素,当调用 GetTemplateChild 后,它将被加载-->
<ContentPresenter x:Name="TitlePresenter" x:DeferLoadStrategy="Lazy" Content="{TemplateBinding Title}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Foreground="White" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <!--对应的 ControlTemplate 中的延迟加载元素 TitlePresenter 不会被加载-->
<local:TitledImage Source="/Assets/SplashScreen.png" /> <!--对应的 ControlTemplate 中的延迟加载元素 TitlePresenter 会被加载(因为调用了 GetTemplateChild)-->
<local:TitledImage Source="/Assets/SplashScreen.png" Title="image title" /> </StackPanel>
</Grid>
</Page>

Xaml/DeferLoadStrategy/Demo5.xaml.cs

/*
* 演示 x:DeferLoadStrategy 的相关知识点
*
* 本例演示通过“GetTemplateChild”加载延迟加载元素
*/ using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging; namespace Windows10.Xaml.DeferLoadStrategy
{
public sealed partial class Demo5 : Page
{
public Demo5()
{
this.InitializeComponent();
}
} // 自定义控件(一个可显示 Title 的 Image)
public class TitledImage : Control
{
// 此自定义控件用于显示 Title 的 ContentPresenter
private ContentPresenter _titlePresenter; public TitledImage()
{
this.DefaultStyleKey = typeof(TitledImage); // 注册一个回调,当 Title 发生变化时触发
this.RegisterPropertyChangedCallback(TitleProperty, TitleChanged);
} // 定义一个依赖属性 Title(用于显示标题)
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(nameof(Title), typeof(string), typeof(TitledImage), new PropertyMetadata(null));
public string Title
{
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
} // 定义一个依赖属性 Source(用于显示图片)
public static readonly DependencyProperty SourceProperty = DependencyProperty.Register(nameof(Source), typeof(ImageSource), typeof(TitledImage), new PropertyMetadata(null));
public ImageSource Source
{
get { return (BitmapSource)GetValue(SourceProperty); }
set { SetValue(SourceProperty, value); }
} void TitleChanged(DependencyObject sender, DependencyProperty prop)
{
string title = (string)sender.GetValue(prop); if (!string.IsNullOrEmpty(Title) && _titlePresenter == null)
{
// TitlePresenter 是控件模板中的一个延迟加载元素,通过 GetTemplateChild 调用后,它将被加载
_titlePresenter = (ContentPresenter)GetTemplateChild("TitlePresenter");
}
} protected override void OnApplyTemplate()
{
base.OnApplyTemplate(); if (!string.IsNullOrEmpty(Title))
{
// TitlePresenter 是控件模板中的一个延迟加载元素,通过 GetTemplateChild 调用后,它将被加载
_titlePresenter = (ContentPresenter)GetTemplateChild("TitlePresenter");
}
}
}
}

6、x:Null - null
Xaml/NullDemo.xaml

<Page
x:Class="Windows10.Xaml.NullDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <TextBlock Name="lblMsg" TextWrapping="Wrap" Margin="5" /> <!--不指定 Tag 则其默认值为 null-->
<TextBlock Name="textBlock1" Margin="5" /> <!--指定 Tag 的值为 ""-->
<TextBlock Name="textBlock2" Margin="5" Tag="" /> <!--指定 Tag 的值为 null-->
<TextBlock Name="textBlock3" Margin="5" Tag="{x:Null}" /> </StackPanel>
</Grid>
</Page>

Xaml/NullDemo.xaml.cs

/*
* 演示 x:Null 的相关知识点、
*
* 在这里插一句:
* 在 xaml 使用的 {x:Null}, {Binding}, {x:Bind}, {StaticResource} 之类的这种带大括号的语法被称为标记扩展(Markup Extension),在 uwp 中无法开发自定义标记扩展(但是在 wpf 中是可以的)
*/ using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace Windows10.Xaml
{
public sealed partial class NullDemo : Page
{
public NullDemo()
{
this.InitializeComponent(); this.Loaded += NullDemo_Loaded;
} private void NullDemo_Loaded(object sender, RoutedEventArgs e)
{
lblMsg.Text = $"textBlock1.Tag: {textBlock1.Tag ?? "null"}"; // null
lblMsg.Text += Environment.NewLine; lblMsg.Text += $"textBlock2.Tag: {textBlock2.Tag ?? "null"}"; // ""
lblMsg.Text += Environment.NewLine; lblMsg.Text += $"textBlock3.Tag: {textBlock3.Tag ?? "null"}"; // null
}
}
}

OK
[源码下载]

背水一战 Windows 10 (26) - XAML: x:DeferLoadStrategy, x:Null的更多相关文章

  1. 背水一战 Windows 10 (27) - 控件(文本类): TextBlock

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

  2. 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件

    [源码下载] 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件 作者 ...

  3. 背水一战 Windows 10 (13) - 绘图: Stroke, Brush

    [源码下载] 背水一战 Windows 10 (13) - 绘图: Stroke, Brush 作者:webabcd 介绍背水一战 Windows 10 之 绘图 Stroke - 笔划 Brush ...

  4. 背水一战 Windows 10 (12) - 绘图: Shape, Path

    [源码下载] 背水一战 Windows 10 (12) - 绘图: Shape, Path 作者:webabcd 介绍背水一战 Windows 10 之 绘图 Shape - 图形 Path - 路径 ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Net作业调度(一) -Quartz.Net入门

    背景 很多时候,项目需要在不同时刻,执行一个或很多个不同的作业. Windows执行计划这时并不能很好的满足需求了,迫切需要一个更为强大,方便管理,集群部署的作业调度框架. 介绍 Quartz一个开源 ...

  2. ASP.NET MVC 控制器激活(三)

    ASP.NET MVC 控制器激活(三) 前言 在上个篇幅中说到从控制器工厂的GetControllerInstance()方法来执行控制器的注入,本篇要讲是在GetControllerInstanc ...

  3. 分享自己写的JS版日期格式化和解析工具类,绝对好用!

    前言 本来想模仿Java里面的SimpleDateFormat()对象的,但是感觉这样用起来不方便,所以还是直接写成单独的方法算了. 原文链接 日期格式化 使用说明 formatDate(date, ...

  4. C#+OpenGL+FreeType显示3D文字(2) - 用GLSL+VBO绘制文字

    C#+OpenGL+FreeType显示3D文字(2) - 用GLSL+VBO绘制文字 +BIT祝威+悄悄在此留下版了个权的信息说: 上一篇得到了字形贴图及其位置字典(可导出为XML).本篇就利用此贴 ...

  5. Step by step 活动目录中添加一个子域

    原创地址:http://www.cnblogs.com/jfzhu/p/4006545.html 转载请注明出处 前面介绍过如何创建一个域,下面再介绍一下如何在该父域中添加一个子域. 活动目录中的森林 ...

  6. java即时通信小例子

    学习java一段时间了,今天写来一个即时通信的小例子练手在其过程中也学到了一些知识拿出来和大家分享,请路过的各位大神多多赐教... 好了下面讲一下基本的思路: 首先,编写服务器端的程序,简单点说吧就是 ...

  7. Android开发学习之路-Android N新特性-多窗口模式

    我们都知道,在最新的Android N系统中,加入了一个新的功能,就是多窗口模式.多窗口模式允许我们在屏幕上显示两个窗口,每个窗口显示的内容不同,也就是说,我们可以一遍看电视剧,一边聊微信. 这里我们 ...

  8. Android开发学习之路-二维码学习

    这个月装逼有点少了,为什么呢,因为去考软件射鸡师了,快到儿童节了,赶紧写篇博纪念一下逝去的青春,唔,请忽略这句话. 二维码其实有很多种,但是我们常见的微信使用的是一种叫做QRCode的二维码,像下面这 ...

  9. ATM模拟器(附代码及运行结果)

    源代码: import java.util.Scanner; class Account{ String identify; String name; String date; String key; ...

  10. 【转】WPF DataGrid 获取选中的当前行某列值

    方法一:DataRowView mySelectedElement = (DataRowView)dataGrid1.SelectedItem; string result = mySelectedE ...