Xamarin.Forms应用程序可以使用DynamicResource标记扩展在运行时动态响应样式更改。

此标记扩展类似于StaticResource标记扩展,两者都使用字典键从ResourceDictionary中获取值。 但是,在StaticResource标记扩展执行单个字典查找的同时,DynamicResource标记扩展维护与字典键的链接。 因此,如果替换了与键关联的值,则更改将应用于VisualElement。 这使得可以在Xamarin.Forms应用程序中实现运行时主题。

在Xamarin.Forms应用程序中实现运行时主题的过程如下:

  1. 创建一个xaml文件,继承ResourceDictionary,并在其中为每个主题定义资源
  2. 使用DynamicResource标记扩展在应用程序中使用主题资源
  3. 在应用程序的App.xaml文件中设置默认主题
  4. 添加代码以在运行时加载主题。

定义主题

创建xaml文件,主题定义为存储在ResourceDictionary中的资源对象的集合。

以下示例显示了示例应用程序中的LightTheme:

<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ThemingDemo.LightTheme">
<Color x:Key="PageBackgroundColor">White</Color>
<Color x:Key="NavigationBarColor">WhiteSmoke</Color>
<Color x:Key="PrimaryColor">WhiteSmoke</Color>
<Color x:Key="SecondaryColor">Black</Color>
<Color x:Key="PrimaryTextColor">Black</Color>
<Color x:Key="SecondaryTextColor">White</Color>
<Color x:Key="TertiaryTextColor">Gray</Color>
<Color x:Key="TransparentColor">Transparent</Color>
</ResourceDictionary>

每个ResourceDictionary包含定义各自主题的Color资源,每个ResourceDictionary使用相同的键值。 有关资源字典的更多信息,请参见资源字典

注:每个ResourceDictionary都需要cs隐藏代码,该代码调用InitializeComponent方法,这是必需的,以便可以在运行时创建表示所选主题的CLR对象。

设置默认主题

应用程序需要默认主题,以便控件具有它们消耗的资源的值。 可以通过将主题的ResourceDictionary合并到App.xaml中定义的应用程序级ResourceDictionary来设置默认主题:

<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ThemingDemo.App">
<Application.Resources>
<ResourceDictionary Source="Themes/LightTheme.xaml" />
</Application.Resources>
</Application>

使用主题资源

当应用程序要使用存储在代表主题的ResourceDictionary中的资源时,应使用DynamicResource标记扩展名进行操作。 这样可以确保如果在运行时选择了其他主题,则将应用新主题中的值。

下面的示例显示了示例 应用程序中可以应用于Label对象的三种样式:

<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ThemingDemo.App">
<Application.Resources> <Style x:Key="LargeLabelStyle"
TargetType="Label">
<Setter Property="TextColor"
Value="{DynamicResource SecondaryTextColor}" />
<Setter Property="FontSize"
Value="30" />
</Style> <Style x:Key="MediumLabelStyle"
TargetType="Label">
<Setter Property="TextColor"
Value="{DynamicResource PrimaryTextColor}" />
<Setter Property="FontSize"
Value="25" />
</Style> <Style x:Key="SmallLabelStyle"
TargetType="Label">
<Setter Property="TextColor"
Value="{DynamicResource TertiaryTextColor}" />
<Setter Property="FontSize"
Value="15" />
</Style> </Application.Resources>
</Application>

这些样式在应用程序级 资源字典中定义(主题的xaml文件中),以便它们可以被多个页面使用。 每种样式都使用DynamicResource标记扩展来消耗主题资源。

这些样式随后被页面使用:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ThemingDemo"
x:Class="ThemingDemo.UserSummaryPage"
Title="User Summary"
BackgroundColor="{DynamicResource PageBackgroundColor}">
...
<ScrollView>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="200" />
<RowDefinition Height="120" />
<RowDefinition Height="70" />
</Grid.RowDefinitions>
<Grid BackgroundColor="{DynamicResource PrimaryColor}">
<Label Text="Face-Palm Monkey"
VerticalOptions="Center"
Margin="15"
Style="{StaticResource MediumLabelStyle}" />
...
</Grid>
<StackLayout Grid.Row="1"
Margin="10">
<Label Text="This monkey reacts appropriately to ridiculous assertions and actions."
Style="{StaticResource SmallLabelStyle}" />
<Label Text=" • Cynical but not unfriendly."
Style="{StaticResource SmallLabelStyle}" />
<Label Text=" • Seven varieties of grimaces."
Style="{StaticResource SmallLabelStyle}" />
<Label Text=" • Doesn't laugh at your jokes."
Style="{StaticResource SmallLabelStyle}" />
</StackLayout>
...
</Grid>
</ScrollView>
</ContentPage>

直接使用主题资源时,应使用DynamicResource标记扩展来使用。 但是,当使用了使用DynamicResource标记扩展的样式时,应将其与StaticResource标记扩展一起使用。

在运行时加载主题

在运行时选择主题时,应用程序应:

  1. 从应用程序中删除当前主题。 通过清除应用程序级ResourceDictionary的MergedDictionaries属性来实现。
  2. 加载所选主题。 这是通过将所选主题的实例添加到应用程序级ResourceDictionary的MergedDictionaries属性中来实现的。

任何使用DynamicResource标记扩展设置属性的VisualElement对象都将应用新的主题值。 发生这种情况是因为DynamicResource标记扩展保留了到字典键的链接。 因此,当替换与键关联的值时,所做的更改将应用于VisualElement对象。

在示例应用程序中,通过包含Picker的模式页面选择主题。 以下代码显示了OnPickerSelectionChanged方法,该方法在选定主题更改时执行:

void OnPickerSelectionChanged(object sender, EventArgs e)
{
Picker picker = sender as Picker;
Theme theme = (Theme)picker.SelectedItem; ICollection<ResourceDictionary> mergedDictionaries = Application.Current.Resources.MergedDictionaries;
if (mergedDictionaries != null)
{
mergedDictionaries.Clear(); switch (theme)
{
case Theme.Dark:
mergedDictionaries.Add(new DarkTheme());
break;
case Theme.Light:
default:
mergedDictionaries.Add(new LightTheme());
break;
}
}
}

Xamarin.Forms之主题的更多相关文章

  1. Xamarin.Forms 简介

    An Introduction to Xamarin.Forms 来源:http://developer.xamarin.com/guides/cross-platform/xamarin-forms ...

  2. 张高兴的 Xamarin.Forms 开发笔记:Android 快捷方式 Shortcut 应用

    一.Shortcut 简介 Shortcut 是 Android 7.1 (API Level 25) 的新特性,类似于苹果的 3D Touch ,但并不是压力感应,只是一种长按菜单.Shortcut ...

  3. Xamarin.Forms之页面及导航

    参考链接: Xamarin. Forms 页面 Xamarin.Forms 导航 Xamarin.Forms 第04局:页面 Xamarin.Forms页面代表跨平台的移动应用程序屏幕. 下文描述的所 ...

  4. 【Xamarin.Forms 2】App基础知识与App启动

    系列目录 1.[Xamarin.Forms 1]App的创建与运行 引言 本篇文章将介绍Xamarin.Forms中 App 基础知识和 App的启动. 开发环境 Visual Studio 2019 ...

  5. xamarin.forms新建项目android编译错误

    vs2015 update3 新建的xamarin.forms项目中的android项目编译错误.提示缺少android_m2repository_r22.zip,96659D653BDE0FAEDB ...

  6. Xamarin.Forms 免费电子书

    Xamarin Evolve 正在举行,现在已经放出2本免费的Xamarin.Forms 免费电子书,据现场的同学说这两天还有Xamarin.Forms 重磅消息发布: Creating Mobile ...

  7. 老司机学新平台 - Xamarin Forms开发框架之MvvmCross插件精选

    在前两篇老司机学Xamarin系列中,简单介绍了Xamarin开发环境的搭建以及Prism和MvvmCross这两个开发框架.不同的框架,往往不仅仅使用不同的架构风格,同时社区活跃度不同,各种功能模块 ...

  8. 老司机学新平台 - Xamarin Forms开发框架二探 (Prism vs MvvmCross)

    在上一篇Xamarin开发环境及开发框架初探中,曾简单提到MvvmCross这个Xamarin下的开发框架.最近又评估了一些别的,发现老牌Mvvm框架Prism现在也支持Xamarin Forms了, ...

  9. 使用Xamarin.Forms平台开发移动应用指南

    下载书:链接: http://pan.baidu.com/s/1c29H9KG 密码: 7esm 注:捣鼓虚拟机把Hyper-V关闭,后来Xamarin搞挂了,所以暂停翻译. 第1章 Xamarin. ...

随机推荐

  1. Replication:distribution 中一直在运行 waitfor delay @strdelaytime 语句

    Replication 自动创建来一个 Job:Replication monitoring refresher for distribution,这个Agent执行一个sp: dbo.sp_repl ...

  2. Python 中拼音库 PyPinyin 的用法【华为云技术分享】

    [摘要] 最近碰到了一个问题,项目中很多文件都是接手过来的中文命名的一些素材,结果在部署的时候文件名全都乱码了,导致项目无法正常运行. 后来请教了一位大佬怎么解决文件名乱码的问题,他说这个需要正面解决 ...

  3. 卸载webpack,降低版本

    卸载:npm uninstall webpack -g 重新安装:npm install webpack@3.7.1 -g

  4. 【vue】搭建vue环境以及要安装的所有东西

    参考地址: https://www.cnblogs.com/laizhouzhou/p/8027908.html

  5. .net代码混淆

    本人主要记录一下学习心得,.net关于代码混淆的知识 1.代码混淆的原理,转载链接 2.代码混淆工具,ConfuserEx的使用,转载地址

  6. 电脑远程连接windows阿里云服务器解决卡顿【小白教程】

    我们在阿里云服务器网页上进行远程连接进行操作,会卡顿.解决办法如下: 1.登录阿里云服务器,进入服务器控制台,复制服务器IP:  2.回到桌面,打开cmd命令窗口,输入mstsc  3.在‘’计算机’ ...

  7. 【JVM】CMS垃圾回收器

    一.简介 Concurrent Mark Sweep,是一种以获取最短回收停顿时间为目标的收集器,尤其重视服务的响应速度. CMS是老年代垃圾回收器,基于标记-清除算法实现.新生代默认使用ParNew ...

  8. 剑指前端(前端入门笔记系列)——BOM

    BOM ECMAScript是JavaScript的核心,但如果要在Web中使用JavaScript,那么BOM(浏览器对象模型)则无疑才是真正的核心,BOM提供了很多对象,用于访问浏览器的功能,这些 ...

  9. android Camera 之 ZSL

    ZSL的概念 ZSL (zero shutter lag) 中文名称为零延时拍照,是为了减少拍照延时,让拍照&回显瞬间完成的一种技术. Single Shot 当开始预览后,sensor 和  ...

  10. Android-----CheckBox复选使用(实现简单选餐)

    直接上代码: xml布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmln ...