linq - Xamarin Forms - 如何从 MergedDictionaries 中选择 C# 中的资源
问题描述
有人可以向我展示用于在下面的 IconResources.xaml 文件中选择 AdjustmentsIcon 样式的 Linq 查询吗?
我知道你可以到达...
Application.Current.Resources["key"]
但我正在寻找一种代码有效的方法来使用 Linq 从 MergeDictionary 中选择样式。
应用程序.xaml
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:font="clr-namespace:WP.Device.Resources"
xmlns:resources="clr-namespace:WP.Device.Resources"
x:Class="WP.Device.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<resources:IconResources />
<resources:ColorResources />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
IconResources.xaml
<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:font="clr-namespace:WP.Device.Resources"
xmlns:resources="clr-namespace:WP.Device.Resources"
x:Class="WP.Device.Resources.IconResources">
<ResourceDictionary.MergedDictionaries>
<resources:ColorResources />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="Label" x:Key="AddNewIcon">
<Setter Property="FontSize" Value="30" />
</Style>
<Style TargetType="Label" x:Key="AdjustmentsIcon">
<Setter Property="FontSize" Value="40" />
</Style>
</ResourceDictionary>
更新
我很欣赏@pinedax
的回答,但对我来说......
Application.Current.Resources["key"]
没有来自合并字典的键。我无法制定一个 Linq 查询来找到我的风格,但我写了以下有效的...
public Style FindStyle(ResourceDictionary resourceDictionary, string key)
{
Style style = resourceDictionary.ContainsKey(key) ? resourceDictionary[key] as Style : null;
if (style == null)
{
foreach (var mergedDictionary in resourceDictionary.MergedDictionaries)
{
style = FindStyle(mergedDictionary, key);
if (style != null) break;
}
}
return style;
}
并被称为...
Style errorIcon = FindStyle(Application.Current.Resources, "AddNewIcon");
解决方案
推荐阅读
- java - 由于将空值传递给 FirebaseDatabase.getReference() 导致应用程序崩溃
- gremlin - Gremlin 查询以返回所有边缘属性以及 inV 和 outV
- javascript - 将值/日期数组列表减少到最后 x 天并添加缺失天数
- c++ - 隐藏头文件的内容
- xcode - Xcode 在发布构建存档时抛出错误“在搜索路径中找不到包含的文件 '../plugins-release.xcconfig'(在目标中)”
- r - 在 R 中使用 SPSS 创建数据表
- android - 关于从 onResume() 获取视图的宽度
- r - 将数字字段存储在具有正确小数位的数据文件中
- postgresql - Postgres SQL 查询,它将对嵌套 JSON 对象中的字段进行分组
- javascript - 如何更改 json 响应中的数据