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

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

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

  1. using Windows.Foundation;
  2. using Windows.Foundation.Metadata;
  3.  
  4. namespace Windows.UI.Xaml
  5. {
  6. //
  7. // Summary:
  8. // Represents a declarative rule that applies visual states based on window properties.
  9. [Composable(typeof(IAdaptiveTriggerFactory), CompositionType.Public, , "Windows.Foundation.UniversalApiContract")]
  10. [ContractVersion(typeof(UniversalApiContract), )]
  11. [MarshalingBehavior(MarshalingType.Agile)]
  12. [Static(typeof(IAdaptiveTriggerStatics), , "Windows.Foundation.UniversalApiContract")]
  13. [Threading(ThreadingModel.Both)]
  14. [WebHostHidden]
  15. public class AdaptiveTrigger : StateTriggerBase, IAdaptiveTrigger
  16. {
  17. //
  18. // Summary:
  19. // Initializes a new instance of the AdaptiveTrigger class
  20. public AdaptiveTrigger();
  21.  
  22. //
  23. // Summary:
  24. // Identifies the MinWindowHeight dependency property.
  25. //
  26. // Returns:
  27. // The identifier for the MinWindowHeight dependency property.
  28. public static DependencyProperty MinWindowHeightProperty { get; }
  29. //
  30. // Summary:
  31. // Identifies the MinWindowWidth dependency property.
  32. //
  33. // Returns:
  34. // The identifier for the MinWindowWidth dependency property.
  35. public static DependencyProperty MinWindowWidthProperty { get; }
  36. //
  37. // Summary:
  38. // Gets or sets the minimum window height at which the VisualState should be applied.
  39. //
  40. // Returns:
  41. // The minimum window height (in effective pixels) at which the VisualState should
  42. // be applied.
  43. public System.Double MinWindowHeight { get; set; }
  44. //
  45. // Summary:
  46. // Gets or sets the minimum window width at which the VisualState should be applied.
  47. //
  48. // Returns:
  49. // The minimum window width (in effective pixels) at which the VisualState should
  50. // be applied.
  51. public System.Double MinWindowWidth { get; set; }
  52. }
  53. }

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

  1. enum DeviceType
  2. {
  3. Unknown = ,
  4.  
  5. Desktop = ,
  6.  
  7. Mobile = ,
  8. }
  9.  
  10. using Windows.Foundation.Collections;
  11. using Windows.UI.Xaml;
  12.  
  13. namespace CustomStateTrigger
  14. {
  15. class DeviceTypeAdaptiveTrigger : StateTriggerBase
  16. {
  17. public DeviceType PlatformType
  18. {
  19. get { return (DeviceType)GetValue(PlatformTypeProperty); }
  20. set { SetValue(PlatformTypeProperty, value); }
  21. }
  22.  
  23. public static readonly DependencyProperty PlatformTypeProperty =
  24. DependencyProperty.Register("PlatformType",
  25. typeof(DeviceType),
  26. typeof(DeviceTypeAdaptiveTrigger),
  27. new PropertyMetadata(DeviceType.Unknown, OnDeviceTypePropertyChanged));
  28.  
  29. private static void OnDeviceTypePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  30. {
  31. DeviceTypeAdaptiveTrigger dat = (DeviceTypeAdaptiveTrigger)d;
  32.  
  33. DeviceType type = (DeviceType)e.NewValue;
  34.  
  35. IObservableMap<string,string> qualifiers =
  36. Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().QualifierValues;
  37.  
  38. if(dat != null)
  39. {
  40. if(qualifiers.ContainsKey("DeviceFamily") &&
  41. qualifiers["DeviceFamily"] == "Mobile")
  42. {
  43. dat.SetActive(type == DeviceType.Mobile);
  44. }
  45.  
  46. if (qualifiers.ContainsKey("DeviceFamily") &&
  47. qualifiers["DeviceFamily"] == "Desktop")
  48. {
  49. dat.SetActive(type == DeviceType.Desktop);
  50. }
  51. }
  52. }
  53. }
  54. }

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

  1. <Page
  2. x:Class="CustomStateTrigger.MainPage"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:CustomStateTrigger"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. xmlns:Triggers="using:CustomStateTrigger"
  9. mc:Ignorable="d">
  10.  
  11. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  12. <TextBlock x:Name="DeviceTextBlock" VerticalAlignment="Center" HorizontalAlignment="Center" />
  13.  
  14. <VisualStateManager.VisualStateGroups>
  15. <VisualStateGroup x:Name="DeviceState">
  16. <VisualState x:Name="DesktopState">
  17. <VisualState.StateTriggers>
  18. <Triggers:DeviceTypeAdaptiveTrigger PlatformType="Desktop" />
  19. </VisualState.StateTriggers>
  20. <VisualState.Setters>
  21. <Setter Target="DeviceTextBlock.Text" Value="Desktop" />
  22. </VisualState.Setters>
  23. </VisualState>
  24. <VisualState x:Name="MobileState">
  25. <VisualState.StateTriggers>
  26. <Triggers:DeviceTypeAdaptiveTrigger PlatformType="Mobile" />
  27. </VisualState.StateTriggers>
  28. <VisualState.Setters>
  29. <Setter Target="DeviceTextBlock.Text" Value="Mobile" />
  30. </VisualState.Setters>
  31. </VisualState>
  32. </VisualStateGroup>
  33. </VisualStateManager.VisualStateGroups>
  34. </Grid>
  35. </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. 12月5日PHPCMS替换主页

    cms替换主页的步骤 1.先做好静态页面: 2.在D:\wamp\www\phpcms\install_package\phpcms\templates文件夹下建新的文件夹tianqiwangluo( ...

  2. ORM之殇,我们需要什么样的ORM框架?

    最近在研究ORM,究竟什么样的框架才是我们想要的 开发框架的意义在于 开发更标准,更统一,不会因为不同人写的代码不一样 开发效率更高,无需重新造轮子,重复无用的代码,同时简化开发流程 运行效率得到控制 ...

  3. 华硕win10文档类文件点击右键时会闪一下,没法用右键打开文件

    华硕的win10系统,把系统自带的福昕软件Foxit PhantomPDF卸载了就好了

  4. Oracle 中 decode 函数用法

    Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译 ...

  5. js自动轮播图片的两种循环方法(原创)

    用5个div,布局从左到右5张图片,从左到右5个div分别指定ID为img1,img2,img3,img4,img5.(背景是relative,5个div是相对于背景absolute定位) 显示如下: ...

  6. 1. web前端开发分享-css,js入门篇

    关注前端这么多年,没有大的成就,就入门期间积累了不少技巧与心得,跟大家分享一下,不一定都适合每个人,毕竟人与人的教育背景与成长环境心理活动都有差别,但就别人的心得再结合自己的特点,然后探索适合自己的学 ...

  7. 吐槽贴:百度地图 api 封装 的实用功能 [源码下载]

    ZMap 类 功能介绍 ZMap 是学习百度地图 api 接口,开发基本功能后整的一个脚本类,本类方法功能大多使用 prototype 原型 实现: 包含的功能有:轨迹回放,圈画区域可编辑,判断几个坐 ...

  8. python --> 正则表达式

    在python中使用正则表达式,需要导入 re 模块 一. 元字符,包括 []  {} | ? * +  .  ^ $ \  () . 号:通配符,一个点号就代表一个字符,一般情况下不能通配换行符 \ ...

  9. 【UWP】解析GB2312、GBK编码网页乱码问题

    在WebHttpRequest请求网页后,获取到的中文是乱码,类似这样: <title>˹ŵ��Ϸ���������� - ��̳������ -  ˹ŵ��Ϸ����</title ...

  10. Android Studio导入第三方类库的方法

    Android Studio导入第三方类库的方法 本人也刚刚开始尝试做android app的开发,听说android studio是Google支持的android 应用开发工具,所以想应该肯定比E ...