这篇博客将介绍在UWP程序中如何创建和使用自定义VisualState Trigger。

上一篇博客中介绍了如何使用AdaptiveTrigger。目前UWP内置的StateTrigger只有AdaptiveTrigger一个,当MinWindowWidth/MinWindowHeight发生改变时,执行一些自适应布局。现在有这样一个场景,UWP程序在Mobile和Desktop上运行时显示不同的文字,这个时候就需要用到自定义StateTrigger了。

先分析一下系统自带的AdaptiveTrigger,

using Windows.Foundation;
using Windows.Foundation.Metadata; namespace Windows.UI.Xaml
{
//
// Summary:
// Represents a declarative rule that applies visual states based on window properties.
[Composable(typeof(IAdaptiveTriggerFactory), CompositionType.Public, , "Windows.Foundation.UniversalApiContract")]
[ContractVersion(typeof(UniversalApiContract), )]
[MarshalingBehavior(MarshalingType.Agile)]
[Static(typeof(IAdaptiveTriggerStatics), , "Windows.Foundation.UniversalApiContract")]
[Threading(ThreadingModel.Both)]
[WebHostHidden]
public class AdaptiveTrigger : StateTriggerBase, IAdaptiveTrigger
{
//
// Summary:
// Initializes a new instance of the AdaptiveTrigger class
public AdaptiveTrigger(); //
// Summary:
// Identifies the MinWindowHeight dependency property.
//
// Returns:
// The identifier for the MinWindowHeight dependency property.
public static DependencyProperty MinWindowHeightProperty { get; }
//
// Summary:
// Identifies the MinWindowWidth dependency property.
//
// Returns:
// The identifier for the MinWindowWidth dependency property.
public static DependencyProperty MinWindowWidthProperty { get; }
//
// Summary:
// Gets or sets the minimum window height at which the VisualState should be applied.
//
// Returns:
// The minimum window height (in effective pixels) at which the VisualState should
// be applied.
public System.Double MinWindowHeight { get; set; }
//
// Summary:
// Gets or sets the minimum window width at which the VisualState should be applied.
//
// Returns:
// The minimum window width (in effective pixels) at which the VisualState should
// be applied.
public System.Double MinWindowWidth { get; set; }
}
}

AdaptiveTrigger继承了StateTriggerBase,那么我们自定义的State Trigger继承StateTriggerBase即可。

enum DeviceType
{
Unknown = , Desktop = , Mobile = ,
} using Windows.Foundation.Collections;
using Windows.UI.Xaml; namespace CustomStateTrigger
{
class DeviceTypeAdaptiveTrigger : StateTriggerBase
{
public DeviceType PlatformType
{
get { return (DeviceType)GetValue(PlatformTypeProperty); }
set { SetValue(PlatformTypeProperty, value); }
} public static readonly DependencyProperty PlatformTypeProperty =
DependencyProperty.Register("PlatformType",
typeof(DeviceType),
typeof(DeviceTypeAdaptiveTrigger),
new PropertyMetadata(DeviceType.Unknown, OnDeviceTypePropertyChanged)); private static void OnDeviceTypePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
DeviceTypeAdaptiveTrigger dat = (DeviceTypeAdaptiveTrigger)d; DeviceType type = (DeviceType)e.NewValue; IObservableMap<string,string> qualifiers =
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().QualifierValues; if(dat != null)
{
if(qualifiers.ContainsKey("DeviceFamily") &&
qualifiers["DeviceFamily"] == "Mobile")
{
dat.SetActive(type == DeviceType.Mobile);
} if (qualifiers.ContainsKey("DeviceFamily") &&
qualifiers["DeviceFamily"] == "Desktop")
{
dat.SetActive(type == DeviceType.Desktop);
}
}
}
}
}

这样我们自定义的一个StateTrigger就完成了。下面在XAML中应用这个自定义的StateTrigger。

<Page
x:Class="CustomStateTrigger.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:CustomStateTrigger"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Triggers="using:CustomStateTrigger"

mc:Ignorable="d"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock x:Name="DeviceTextBlock" VerticalAlignment="Center" HorizontalAlignment="Center" /> <VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="DeviceState">
<VisualState x:Name="DesktopState">
<VisualState.StateTriggers>
<Triggers:DeviceTypeAdaptiveTrigger PlatformType="Desktop" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="DeviceTextBlock.Text" Value="Desktop" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="MobileState">
<VisualState.StateTriggers>
<Triggers:DeviceTypeAdaptiveTrigger PlatformType="Mobile" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="DeviceTextBlock.Text" Value="Mobile" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</Page>

运行结果:

在Github上有一个UWP的WindowsStateTriggers,里面都是一些自定义的StateTrigger,

https://github.com/dotMorten/WindowsStateTriggers

[UWP]创建自定义VisualState Trigger的更多相关文章

  1. 我的面板我做主 -- 淘宝UWP中自定义Panel的实现

    在Windows10 UWP开发平台上内置的XMAL布局面板包括RelativePanel.StackPanel.Grid.VariableSizedWrapGrid 和 Canvas.在开发淘宝UW ...

  2. ArcGIS Engine环境下创建自定义的ArcToolbox Geoprocessing工具

    在上一篇日志中介绍了自己通过几何的方法合并断开的线要素的ArcGIS插件式的应用程序.但是后来考虑到插件式的程序的配置和使用比较繁琐,也没有比较好的错误处理机制,于是我就把之前的程序封装成一个类似于A ...

  3. UWP 扩展/自定义标题栏的方法,一些概念和一些注意事项

    原文 UWP 扩展/自定义标题栏的方法,一些概念和一些注意事项 在 Windows 10 的前几个版本中将页面内容扩展到标题栏上还算简单,主要是没什么坑.直到一些新控件的引入和一些外观设计趋势变化之后 ...

  4. Customize Acrylic Brush in UWP Applications(在UWP中自定义亚克力笔刷)

    原文 Customize Acrylic Brush in UWP Applications(在UWP中自定义亚克力笔刷) Windows 10 Fall Creators Update(Build ...

  5. ASP.NET MVC随想录——创建自定义的Middleware中间件

    经过前2篇文章的介绍,相信大家已经对OWIN和Katana有了基本的了解,那么这篇文章我将继续OWIN和Katana之旅——创建自定义的Middleware中间件. 何为Middleware中间件 M ...

  6. 带你走近AngularJS - 创建自定义指令

    带你走近AngularJS系列: 带你走近AngularJS - 基本功能介绍 带你走近AngularJS - 体验指令实例 带你走近AngularJS - 创建自定义指令 ------------- ...

  7. [转]maven创建自定义的archetype

    创建自己的archetype一般有两种方式,比较简单的就是create from project 1.首先使用eclipse创建一个新的maven project,然后把配置好的一些公用的东西放到相应 ...

  8. Dockerfile创建自定义Docker镜像以及CMD与ENTRYPOINT指令的比较

    1.概述 创建Docker镜像的方式有三种 docker commit命令:由容器生成镜像: Dockerfile文件+docker build命令: 从本地文件系统导入:OpenVZ的模板. 关于这 ...

  9. .NET微信公众号开发-2.0创建自定义菜单

    一.前言 开发之前,我们需要阅读官方的接口说明文档,不得不吐槽一下,微信的这个官方文档真的很烂,但是,为了开发我们需要的功能,我们也不得不去看这些文档. 接口文档地址:http://mp.weixin ...

随机推荐

  1. 移动端 Web 开发前端知识整理

    文章来源: http://www.restran.net/2015/05/14/mobile-web-front-end-collections/ 最近整理的移动端 Web 开发前端知识,不定期更新. ...

  2. Git 总结

    详情请参考:https://git-scm.com/book/zh/v2 注意事项:#1. 多提交(相当于多保存,多^S): 在Git中任何已提交的东西几乎总是可以恢复的. 甚至那些被删除的分支中的提 ...

  3. 没有素描色彩基础适合学习UI吗,如果可以,该怎么学?

      文章背景,来自[ UI设计交流群 - 92588284 ]的一次探讨,由于个人视野有限,个中观点有失狭隘,仅供参考,欢迎拍砖: 问题一:一个朋友想学UI设计,没有任何基础的,也没有美术功底,想问问 ...

  4. 函数式 js 接口实现原理,以及 lodash/fp 模块

    函数式 js 接口 之前在 youtube 上看到一个技术视频,讲“underscore.js的接口为什么不好用”,以及什么样的接口更好用.演讲者是 lodash.js 的作者,他提出了一种“全面函数 ...

  5. JS中用apply、bind实现为函数或者类传入动态个数的参数

    为纪念10年没写blog,第一篇博文就以这样一个有趣的窍门开始吧 -___- 在ES5中,当我们调用一个函数时,如果要传入的参数是根据其他函数或条件判断生成的,也就是说不确定会传入多少个参数时,在不改 ...

  6. <c ss高效开发实战>看完了,Bootstrap学习是关键

    Bootstrap果真给我们带来了很多便利,学习CSS,必须要掌握很多框架和快速学习的方法. 这本书看完了,也写过几篇读书笔记,墙裂推荐.不上书封面了,只上书的导图. 这里说几点学习CSS的心得 1. ...

  7. .NET中的异步

    .NET中4种异步方式? ThreadPool.QueueUserworkItem实现 APM模式(就是BeginXXX和EndXXX成对出现.) EAP模式(就是Event based, 准确说来就 ...

  8. Dijkstra 算法

    all the nodes should be carectorized into three groups: (visited, front, unknown) we should pay spec ...

  9. Django Restful Framework (二): ModelSerializer

    时常,你需要对django model 的实例进行序列化.ModelSerializer 类提供了一个捷径让你可以根据 Model 来创建 Serializer. ModelSerializer 类和 ...

  10. overridePendingTransition简介

    1 Activity的切换动画指的是从一个activity跳转到另外一个activity时的动画. 它包括两个部分:一部分是第一个activity退出时的动画:另外一部分时第二个activity进入时 ...