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
}
}
}

XAML: x:DeferLoadStrategy, x:Null的更多相关文章

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

    [源码下载] 背水一战 Windows 10 (26) - XAML: x:DeferLoadStrategy, x:Null 作者:webabcd 介绍背水一战 Windows 10 之 XAML ...

  2. XAML

    XAML定义 XAML是一种相对简单.通用的声明式编程语言,它适合于构建和初始化.NET对象. XAML仅仅是一种使用.NET API的方式,把它与HTML.可伸缩向量图形(SVG)或其他特定领域的格 ...

  3. XAML 概述三

    通过对前面2节对XAML的介绍,我们对XAML有了一定的认识.这一节我们来简单了解一下部分XAML命名空间(x:)语言功能. x命名空间映射的是http://schemas.microsoft.com ...

  4. XAML 名称范围 (x:) 语言特性

    本节介绍为 Windows 运行时实现的 XAML 语言特性的参考信息. 本部分内容 主题 描述 x:Class 属性 配置 XAML 编译,在标记和代码隐藏之间连接分部类.代码分部类在一个独立的代码 ...

  5. XAML属性赋值转换之谜(WPF XAML语法解密)

    XAML与XML类似,就是XML延伸过来的.为了更好的表达一些功能,WPF对XML做了扩展,有些功能是WPF在后台悄悄的替你做了.有时候,虽然实现了某个功能,但是对实现原理还是很茫然.今天就讲讲XAM ...

  6. WPF,Silverlight与XAML读书笔记(3) - 标记扩展

    hystar的.Net世界 博客园 首页 新闻 新随笔 联系 管理 订阅 随笔- 103  文章- 0  评论- 107  WPF,Silverlight与XAML读书笔记(3) - 标记扩展   说 ...

  7. 对XAML进行编辑的辅助类(XamlHelper)

    原文:对XAML进行编辑的辅助类(XamlHelper) // XamlHelper.cs// --------------------------------------------// 对XAML ...

  8. 《深入理解JAVA虚拟机》笔记1

    java程序运行时的内存空间,按照虚拟机规范有下面几项: )程序计数器 指示下条命令执行地址.当然是线程私有,不然线程怎么能并行的起来. 不重要,占内存很小,忽略不计. )方法区 这个名字很让我迷惑. ...

  9. 【Win10】UAP/UWP/通用 开发之 x:DeferLoadStrategy

    [Some information relates to pre-released product which may be substantially modified before it's co ...

随机推荐

  1. jvm虚拟机性能监控与故障处理工具

    java开发人员肯定知道jdk的bin目录中有java.exe javac.exe这两个命令行工具,但并非所有程序员都了解过jdk的bin目录之中其他命令行的作用.jdk的工具,体积都比较小,这些命令 ...

  2. [No000053]我25岁了,是应该继续挣钱,还是选择自己的爱好?--正好庆祝自己25岁生日

    你所问的问题正是问题所在.停止做出重大决策,专注于缩小你想到达的地位与你之间的差距. 成功的生活并非由简单而鲜明的决定组成,它们更像这幅图: 但悲伤的是,太多人的状态类似于这幅图: 我知道这听上去很显 ...

  3. bzoj1067 降雨量&&vijos1265 暴风雨

    描述 话说这日,李逍遥与阿奴正欲前往桃花源拿寿葫芦,突然电闪雷鸣,天降暴雨,弄得两人措手不及,只得到附近的树洞避雨. "哎,大理不是本应旱灾的吗?怎么会突降暴雨呢?"李逍遥嘀咕道. ...

  4. Java的jar文件安装成windows 服务

    Java的jar文件安装成windows 服务: 1.下载:nssm,复制到jar文件目录下 2. jar文件目录下创建bat文件[run.bat],内容为[java -jar 文件名.jar] 3. ...

  5. iOS APNS配置(转)

    Introduction To send Push notification to an application/device couple you need an unique device tok ...

  6. UML:类图复习-鸡生蛋,蛋生鸡

    这是前一阵<高级软件工程>课堂上,老师随堂出的一道讨论题,随手贴在这里: ps: 今天是520,正好聊一些OoXx,关于爱的扯淡话题:) 题目:“鸡生蛋,蛋孵鸡”,世间万物生生不息,如何用 ...

  7. java的守护线程与非守护线程

    最近重新研究Java基础知识,发现以前太多知识知识略略带过了,比较说Java的线程机制,在Java中有两类线程:User Thread(用户线程).Daemon Thread(守护线程) ,(PS:以 ...

  8. Android的媒体管理框架:Glide

    Glide是一个高效.开源. Android设备上的媒体管理框架,它遵循BSD.MIT以及Apache 2.0协议发布.Glide具有获取.解码和展示视频剧照.图片.动画等功能,它还有灵活的API,这 ...

  9. 架构系列:ASP.NET 项目结构搭建

    我们头开始,从简单的单项目解决方案,逐步添加业务逻辑的约束,从应用逻辑和领域逻辑两方面考虑,从简单的单个项目逐步搭建一个多项目的解决方案.主要内容:(1)搭建应用逻辑和领域逻辑都简单的单项目 (2)为 ...

  10. 【活动】写#听云#原创博文 赢取iPhone 6超级大奖

    移动应用的使用量和重要性与日俱增,用户体验的要求也越来越高.与桌面程序相比,移动应用耗电小,速度慢,但手机用户却希望享受到与桌面程序同样的加载速度.那么如何发现移动应用的性能黑洞,优化移动应用性能,这 ...