我在几个ResourceDicitonaries
中有一堆Colors
,如下所示:
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:s="clr-namespace:System;assembly=netstandard">
<Color x:Key="Color.Background">#301536</Color>
</ResourceDictionary>
我简单地将两者都添加到App.xaml
中,如下所示:
<!--Colors-->
<ResourceDictionary Source="Resources/Colors/Light.xaml"/>
<ResourceDictionary Source="Resources/Colors/Dark.xaml"/>
ResourceDicitonary
文件只是一个.xaml
文件,没有任何捆绑的.cs
,两个字典都设置在App.xaml
上,而不必在MergedDicionaries
组中定义它们。
当试图从代码中访问a Color
时,我无法找到它,看起来它没有添加到资源列表中。
var color = Application.Current.Resources.FirstOrDefault(f => f.Key == "Color.Background")
.Value as Color? ?? Color.Crimson;
WPF中是否有任何方法可以访问像FindResource/TryFindResource
这样的资源?
我还尝试访问/查看MergedDictionaries
的内容,但由于某种原因,合并的字典总是空的。
3条答案
按热度按时间xbp102n01#
多亏了另一个人,我才能构建出这段工作代码:
irtuqstp2#
根据您的描述,您想在APP.xaml中添加多个ResourceDictionary,但没有成功。我找到一篇关于这个问题的文章,您可以看看:
https://nicksnettravels.builttoroam.com/post/2018/09/02/Getting-Started-with-XamarinForms-and-Resource-Dictionaries.aspx
brjng4g33#
或者,如果你想查找父层次结构,你可以这样做:
它的灵感来自ResourcesExtensions的MAUI源代码,不幸的是,这是内部的。