【问题标题】:Does a resource dictionary have to be explicitly referenced?是否必须明确引用资源字典?
【发布时间】:2014-10-23 00:02:31
【问题描述】:

我在 ResourceDictionary 中有以下内容:

<Style x:Key="BasicStyle" TargetType="ContentControl">
    <Setter Property="Width" Value="100" />
    <Setter Property="Height" Value="30" />
</Style>
<Style x:Key="ButtonStyle" TargetType="Button" BasedOn="{StaticResource BasicStyle}">
    <Setter Property="BorderBrush" Value="Orange" />
    <Setter Property="BorderThickness" Value="2" />
    <Setter Property="Foreground" Value="Orange" />
</Style>

然后我尝试使用 MainPage.xaml 中的按钮样式,如下所示:

...但在运行时我得到,“找不到具有名称/键按钮样式的资源

我必须将 MainPage.xaml 正式引入 ResourceDictionary 吗?

是否必须在 MainPage 中或从 App.xaml 中引用 ResourceDirectionary,或者...???

更新

那么对于 App.xaml 中的 Application.Resources 来说,正确的 XML 是什么?

使用下面 robertos 的想法,在我的项目中添加资源字典并将其命名为“GlobalStylesResourceDictionary.xaml”后,我将其添加到 App.xaml:

<ResourceDictionary>
    <ResourceDictionary Source="GlobalStylesResourceDictionary.xaml" />
</ResourceDictionary>

...所以我的整个 App.xaml 是:

<Application x:Class="Photrax.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Photrax">
    <Application.Resources>
        <ai:TelemetryContext x:Key="ApplicationInsightsBootstrapper" xmlns:ai="using:Microsoft.ApplicationInsights"/>
        <ResourceDictionary>
            <ResourceDictionary Source="GlobalStylesResourceDictionary.xaml" />
        </ResourceDictionary>
    </Application.Resources>
</Application>

但是,这样我得到“每个字典条目都必须有一个关联的键。”

然而,这 [in] 直接来自 Microsoft(robertos 从他引用的 ms 页面获取了他在下面显示的 XAML)。那么有什么问题呢?

【问题讨论】:

    标签: xaml windows-store-apps resourcedictionary app.xaml


    【解决方案1】:

    您必须在 MainPage.xaml 或 App.xaml 中引用 ResourceDictionary,使用类似:

    <Application.Resources>
        <ResourceDictionary>
            <!--other resources can be here-->
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="rd1.xaml" />
                <ResourceDictionary Source="rd2.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    

    更多信息:MSDN reference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多