【问题标题】:Retrieve a value from ResourceDictionary in code behind在后面的代码中从 ResourceDictionary 中检索一个值
【发布时间】:2015-11-02 21:02:17
【问题描述】:

我有一个 Windows 10 通用应用程序,其中包含一些用于样式等的资源字典。 在一个这样的资源字典中,我有一种颜色,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>

【问题讨论】:

    标签: xaml windows-10 resourcedictionary uwp


    【解决方案1】:

    你试过了吗

    Application.Current.Resources["MyBlue"]
    

    【讨论】:

    • 资源集合中没有项目。
    【解决方案2】:

    MergedDictionaries 可通过Application.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"];
    

    【讨论】:

    • 你能分享你的 app.xaml 吗?这不应该是必要的。
    • 呃... :confused: 我不知道。这看起来是正确的,您应该可以通过查看应用程序的资源来获得它。
    【解决方案3】:

    您要引用的ResourceDictionary 必须以某种方式拉入应用程序资源中。要么将其合并到您的 Application 的资源中(如果它足够常见,可以一直包含在您的应用程序初始化中),或者将其合并到您的 Page 的资源中(对于第一页初始化)。

    您使用以下语法包含外部ResourceDictionaries

    <Page.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="path/to/resource.xaml" />
            </ResourceDictionary>
    
            <... your other resources for this page go here .../>
        </ResourceDictionary>
    </Page.Resources>
    

    【讨论】:

    • 我正在将它合并到我的应用程序资源中。我可以在 xaml 中引用资源,但不能在后面的代码中引用。
    • 那么(Color)Application.Current.Resources["MyBlue"] 应该这样做。
    【解决方案4】:

    我将尝试总结所有解决方案,因为所有以前的解决方案似乎都包含整个答案的一部分。

    所以主要有两种情况:

    案例A。您的价值在您的App.xaml 的主要ResourceDictionary

    App.xaml 在哪里:

    ...
    <Application.Resources>
        <ResourceDictionary>
            <Color x:Key="backgroundColor">LightBlue</Color>
        </ResourceDictionary>
    </Application.Resources>
    ...
    

    如果是这样,请使用:

    Application.Current.Resources["backgroundColor"]
    

    案例 B. 您的值在外部 ResourceDictionary 中合并到您的 App.xaml

    App.xaml在哪里:

    ...
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Styles/Constants.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    ...
    

    Constants.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>
    

    在这种情况下使用@earthling 解决方案:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多