2

我需要ResourceDictionaries根据某些配置选项在运行时加载和合并代码。Application.OnStartup当然,在运行时合并方法中的字典可以正常工作。

Visual Studio 的设计模式不会加载您的自定义应用程序类,我知道在 DesignMode 中获取字典的唯一方法是将它们合并到 App.xaml 中。

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="SomeDictionary1.xaml" />
            <ResourceDictionary Source="SomeDictionary2.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

然而,这在我目前的情况下是不可行的。所以我要问的是,Visual Studio 实际上做了什么来将合并的字典从 App.xaml 加载到应用程序范围内的设计图面,而无需构建您的自定义 Application 类?以及如何在设计模式下将这些字典从代码合并到应用程序范围以实际解决问题?

4

1 回答 1

1

使用 d: 命名空间和 mc:Ignorable,这是可能的。在设计时,它将应用 Source 属性。在运行时,它将忽略它。(您将在运行时创建一个空的 ResourceDictionary ...)

<Application x:Class="WpfApplication2.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         mc:Ignorable="d"
         StartupUri="MainWindow.xaml">
  <Application.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary d:Source="designTime.xaml" />
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </Application.Resources>
</Application>

谢谢,罗伯

于 2015-01-08T17:59:19.550 回答