Xaml 在代码隐藏中从ResourceDictionary检索值

Xaml 在代码隐藏中从ResourceDictionary检索值,xaml,windows-10,resourcedictionary,uwp,Xaml,Windows 10,Resourcedictionary,Uwp,我有一个Windows10Universal应用程序,里面有一些样式等的资源字典。 在一个这样的资源字典中,我有一种颜色,MyBlue,我想通过代码隐藏来访问它。这是如何实现的 我已经尝试了this.Resources[“MyBlue”],但是由于MyBlue是在单独的资源字典中定义的,而不是直接在页面资源中定义的,所以它在这个集合中不存在 这是我的app.xaml: <Application.Resources> <ResourceDictionary>

我有一个Windows10Universal应用程序,里面有一些样式等的资源字典。 在一个这样的资源字典中,我有一种颜色,
MyBlue
,我想通过代码隐藏来访问它。这是如何实现的

我已经尝试了
this.Resources[“MyBlue”]
,但是由于
MyBlue
是在单独的资源字典中定义的,而不是直接在页面资源中定义的,所以它在这个集合中不存在

这是我的app.xaml:

<Application.Resources>

    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Styles/PodStyles.xaml"/>
            <ResourceDictionary Source="Styles/ButtonStyles.xaml"/>
            <ResourceDictionary Source="Styles/OfferStyles.xaml"/>
            <ResourceDictionary Source="Styles/MyStyles.xaml"/>

            <ResourceDictionary>
                <!--Global View Model Locator-->
                <vm:ViewModelLocator x:Key="Locator"
                                     d:IsDataSource="True"/>
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

</Application.Resources>

您试过了吗

Application.Current.Resources["MyBlue"]

要引用的
ResourceDictionary
必须以某种方式拉入应用程序资源。要么将其合并到
应用程序的资源中(如果它足够常见,可以随时包含在应用程序的初始化中),要么将其合并到
页面的资源中(只需轻轻点击第一页初始化)

使用以下语法包括外部
资源字典

<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="path/to/resource.xaml" />
        </ResourceDictionary>

        <... your other resources for this page go here .../>
    </ResourceDictionary>
</Page.Resources>

可通过
应用程序.Current.Resources.MergedDictionaries
获得合并词典

我能够通过Uri获得适当的ResourceDictionary,然后通过键名访问其中的项:

var mergedDict = Application.Current.Resources.MergedDictionaries.Where(md => md.Source.AbsoluteUri == "ms-resource:///Files/Styles/MyStyles.xaml").FirstOrDefault();
newColor = (Windows.UI.Color)mergedDict["MyBlue"];

我将尝试总结所有的解决方案,因为之前的所有解决方案似乎都有部分答案

所以主要有两种情况:

案例A.您的值位于
App.xaml的主
resourceddictionary

其中
App.xaml
是:

...
<Application.Resources>
    <ResourceDictionary>
        <Color x:Key="backgroundColor">LightBlue</Color>
    </ResourceDictionary>
</Application.Resources>
...
案例B.您的值在外部
资源字典中
合并到您的
App.xaml中

其中
App.xaml
是:

...
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Styles/Constants.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
...
在这种情况下,使用@earthling解决方案:

var mergedDict = Application.Current.Resources.MergedDictionaries.Where(md => md.Source.OriginalString.Equals("Styles/Constants.xaml")).FirstOrDefault();
var color = (Color)mergedDict["pressedColor"];

我正在将其合并到我的应用程序资源中。我可以在xaml中引用资源,但不能在代码隐藏中引用。然后
(Color)Application.Current.resources[“MyBlue”]
应该可以这样做。该资源集合中没有任何项目。你能共享你的app.xaml吗?这没必要。呃…:惑儒:我不知道。这看起来是正确的,您应该能够通过查看应用程序的资源来实现它。
<?xml version="1.0" encoding="utf-8" ?>
<?xaml-comp compile="true" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <Color x:Key="backgroundColor">LightBlue</Color>
</ResourceDictionary>
var mergedDict = Application.Current.Resources.MergedDictionaries.Where(md => md.Source.OriginalString.Equals("Styles/Constants.xaml")).FirstOrDefault();
var color = (Color)mergedDict["pressedColor"];