Xamarin.Forms之主题
Xamarin.Forms应用程序可以使用DynamicResource标记扩展在运行时动态响应样式更改。
此标记扩展类似于StaticResource标记扩展,两者都使用字典键从ResourceDictionary中获取值。 但是,在StaticResource标记扩展执行单个字典查找的同时,DynamicResource标记扩展维护与字典键的链接。 因此,如果替换了与键关联的值,则更改将应用于VisualElement。 这使得可以在Xamarin.Forms应用程序中实现运行时主题。
在Xamarin.Forms应用程序中实现运行时主题的过程如下:
- 创建一个xaml文件,继承ResourceDictionary,并在其中为每个主题定义资源。
- 使用DynamicResource标记扩展在应用程序中使用主题资源。
- 在应用程序的App.xaml文件中设置默认主题。
- 添加代码以在运行时加载主题。
定义主题
创建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标记扩展一起使用。
在运行时加载主题
在运行时选择主题时,应用程序应:
- 从应用程序中删除当前主题。 通过清除应用程序级ResourceDictionary的MergedDictionaries属性来实现。
- 加载所选主题。 这是通过将所选主题的实例添加到应用程序级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之主题的更多相关文章
- Xamarin.Forms 简介
An Introduction to Xamarin.Forms 来源:http://developer.xamarin.com/guides/cross-platform/xamarin-forms ...
- 张高兴的 Xamarin.Forms 开发笔记:Android 快捷方式 Shortcut 应用
一.Shortcut 简介 Shortcut 是 Android 7.1 (API Level 25) 的新特性,类似于苹果的 3D Touch ,但并不是压力感应,只是一种长按菜单.Shortcut ...
- Xamarin.Forms之页面及导航
参考链接: Xamarin. Forms 页面 Xamarin.Forms 导航 Xamarin.Forms 第04局:页面 Xamarin.Forms页面代表跨平台的移动应用程序屏幕. 下文描述的所 ...
- 【Xamarin.Forms 2】App基础知识与App启动
系列目录 1.[Xamarin.Forms 1]App的创建与运行 引言 本篇文章将介绍Xamarin.Forms中 App 基础知识和 App的启动. 开发环境 Visual Studio 2019 ...
- xamarin.forms新建项目android编译错误
vs2015 update3 新建的xamarin.forms项目中的android项目编译错误.提示缺少android_m2repository_r22.zip,96659D653BDE0FAEDB ...
- Xamarin.Forms 免费电子书
Xamarin Evolve 正在举行,现在已经放出2本免费的Xamarin.Forms 免费电子书,据现场的同学说这两天还有Xamarin.Forms 重磅消息发布: Creating Mobile ...
- 老司机学新平台 - Xamarin Forms开发框架之MvvmCross插件精选
在前两篇老司机学Xamarin系列中,简单介绍了Xamarin开发环境的搭建以及Prism和MvvmCross这两个开发框架.不同的框架,往往不仅仅使用不同的架构风格,同时社区活跃度不同,各种功能模块 ...
- 老司机学新平台 - Xamarin Forms开发框架二探 (Prism vs MvvmCross)
在上一篇Xamarin开发环境及开发框架初探中,曾简单提到MvvmCross这个Xamarin下的开发框架.最近又评估了一些别的,发现老牌Mvvm框架Prism现在也支持Xamarin Forms了, ...
- 使用Xamarin.Forms平台开发移动应用指南
下载书:链接: http://pan.baidu.com/s/1c29H9KG 密码: 7esm 注:捣鼓虚拟机把Hyper-V关闭,后来Xamarin搞挂了,所以暂停翻译. 第1章 Xamarin. ...
随机推荐
- Java学习:内部类的概念于分类
内部类的概念于分类 如果一个事物的内部类包含另一个事物,那么这就是一个类内部包含另一个类.例如:身体和心脏的关系,又如:汽车和发动机的关系. 分类 成员内部类 局部内部类(包含匿名内部类) 成员内部类 ...
- pandas mode()填充nan异常问题
df.mode()return的是一个frame,因为可能存在多个总数.那么用mode()来填充nan的时候就要注意了,如果直接 df.fillna(df.mode()) 会发现还是有很多空值没有填充 ...
- SQL server中常用sql语句
--循环执行插入10000条数据 declare @ID intbeginset @ID=1 while @ID<=10000begininsert into table_name values ...
- 八.软件自动化和web测试
1.软件自动化测试 1.1 自动化测试的概念 自动化测试:就是通过测试工具或其他手段,按照测试工程师的预定计划对软件产品进行自动化的测试 软件测试自动化涉及到测试流程.测试体系.自动化编译以 ...
- spark过滤算子+StringIndexer算子出发的一个逻辑bug
问题描述: 在一段spark机器学习的程序中,同时用到了Filter算子和StringIndexer算子,其中StringIndexer在前,filter在后,并且filter是对stringinde ...
- 安卓、ios时间转换成时间戳的形式
将日期转换成时间戳的形式,在安卓和ios不同的系统下转正会有兼容性的问题 安卓系统下Date.parse(new Date('2018-03-30 12:00:00'))会直接转换成时间戳的形式(简单 ...
- 常用模块 - hashlib模块
一.简介 Python的hashlib提供了常见的摘要算法,如MD5.SHA1.SHA224.SHA256.SHA384.SHA512等算法. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过 ...
- html5调用手机震动
在h5里面里面,浏览器对象有个vibrate属性.顾名思义,翻译过来就是震动的意思,这个api属性方法如下: 要调用的例子 if (window.navigator.vibrate) window.n ...
- SpringCloud之监控数据聚合Turbine
前言 SpringCloud 是微服务中的翘楚,最佳的落地方案. 使用 SpringCloud 的 Hystrix Dashboard 组件可以监控单个应用服务的调用情况,但如果是集群环境,可能就 不 ...
- tensorflow提示:No module named ''tensorflow.python.eager".
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/qq_27921205/articl ...