繁体   English   中英

如何在单个 App.xaml 中合并不同的资源?

[英]How to merge different resources in single App.xaml?

我有一个长编码的 App.xaml,其中包含我从 Internet 下载的自定义样式窗口的代码。

<Application x:Class="MyProject.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:MyProject"
             xmlns:sw="clr-namespace:MyProject.StyleableWindow"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
    <SolidColorBrush x:Key="WindowColor" Color="Red"/>
    <SolidColorBrush x:Key="WindowBackColor" Color="#FFC3C3C3"/>
    <SolidColorBrush x:Key="WindowForeColor" Color="Black"/>
    <SolidColorBrush x:Key="WindowForeHighlightColor" Color="WhiteSmoke"/>
................
................
................
................
            <Setter Property="Background" Value="Black"/>
            <Setter Property="BorderBrush" Value="Red"/>
            <Setter Property="Template" Value="{StaticResource WindowTemplate}"/>
        </Style>

昨天,我下载了一个自定义的切换按钮控件。 为了使其发挥作用,我尝试在 App.xaml 中合并以下代码

    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="SwitchTypeToggleButton.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

但它不起作用。 我不断收到以下错误:

每个字典必须有一个关联的键

我的切换按钮控制代码-(存储在 MyProject > Folder1 > Folder2 中)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="SwitchTypeToggleButton"
           TargetType="{x:Type ToggleButton}">

        <Style.Resources>
            <Color x:Key="Color.Additional.LightGrey">#989EA8</Color>
            <Color x:Key="Color.Additional.MediumGrey">#61656B</Color>
............
............

我该怎么做才能摆脱错误? 任何帮助表示赞赏。

app.xaml 中的资源应如下所示:

<Application.Resources>    
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="SwitchTypeToggleButton.xaml" />
    </ResourceDictionary.MergedDictionaries>
    <SolidColorBrush x:Key="WindowColor" Color="Red"/>
    ...
    <Setter Property="Template" Value="{StaticResource WindowTemplate}"/>
  </ResourceDictionary>      
</Application.Resources>

原因是, Application.Resources实际上总是需要是一个ResourceDictionary 只要您使用“简单”资源,就会隐式创建 ResourceDictionary。 但是,只要您想合并另一个 ResourceDictionary,就必须明确指定包含的那个。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM