0

我在 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 中的按钮样式,如下所示:

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

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

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

更新

那么 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>

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

然而,这直接来自微软(罗伯托斯从他引用的 ms 页面中获取了他在下面显示的 XAML)。所以有什么问题?

4

1 回答 1

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 参考

于 2014-10-23T00:06:40.037 回答