这篇博客将介绍在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

最新文章

  1. Extjs.Button 按钮
  2. c#中文件与二进制流文件的转换
  3. Windows批处理:自动检查服务器连通性
  4. Yii2 行为
  5. 从自签名证书导出pfx和cer证书
  6. java微信开发(wechat4j)——设置响应微信参数
  7. 迭代器(Iterator)模式
  8. iOS开发UI篇—程序启动原理和UIApplication1
  9. 小Q书桌的下载、安装和使用
  10. android ToolBar与DrawerLayout笔记
  11. SQL Server 2012学习笔记 2 Server Core中命令行安装SQL
  12. Velocity.js的使用
  13. class中的东西和继承、多态的概念
  14. [Luogu3527][POI2011]MET-Meteors
  15. centOS7配置DNS服务器
  16. ADFS部署过程中设置network service对证书的读取权限
  17. 图表管理账单的NABCD
  18. Linux 查看文件编码
  19. Docker 入门指南——资源工具篇
  20. 6.Python爬虫入门六之Cookie的使用

热门文章

  1. 《你不常用的c#之XX》
  2. spring统一日志管理,切面(@Aspect),注解式日志管理
  3. VirtualBox COM对象获取失败
  4. Python 学习小结
  5. .NET积累
  6. Java的垃圾回收和内存分配策略
  7. HBase 基本shell命令
  8. WPF获取应用程序启动目录的方法
  9. python之路十九
  10. django tag