.net 在设计模式下,App.xaml中合并的ResourceDictionary会发生什么情况?

.net 在设计模式下,App.xaml中合并的ResourceDictionary会发生什么情况?,.net,wpf,visual-studio-2013,resourcedictionary,designmode,.net,Wpf,Visual Studio 2013,Resourcedictionary,Designmode,我需要在运行时根据某些配置选项在代码中加载和合并resourcedictionary。在应用程序.OnStartup方法中合并字典当然可以正常工作,在运行时 Visual studio的设计模式不会加载自定义应用程序类,我所知道的在设计模式下使用词典的唯一方法是将它们合并到App.xaml中 <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDicti

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

Visual studio的设计模式不会加载自定义应用程序类,我所知道的在设计模式下使用词典的唯一方法是将它们合并到App.xaml中

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


然而,在我目前的情况下,这是不可行的。所以我要问的是,visual studio在不构建自定义应用程序类的情况下,如何将合并字典从App.xaml加载到应用程序范围的设计图面?如何在设计模式下将这些字典从代码合并到应用程序范围以实际解决问题?

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



谢谢,Rob

谢谢,但我需要从代码开始,我不确定这是否可行,但我认为应该这样做。我需要它,因为我想自动化从自定义应用程序类中的代码添加这些字典的过程,因此不必接触App.xaml。实际上,“d:Source”有一个问题,“Source不可附加到ResourceDictionary类型的元素”。如果您希望它在设计时工作,您需要以声明的方式执行,而不是从代码中执行。我以为您想从运行时的代码开始,但它的缺点是无法在设计器中工作。我展示的解决方案支持设计时使用可以作为占位符的样式…在运行时,您可以通过编程方式使用相同的RD,或者让代码选择适当的样式。很抱歉,如果它不能完全解决您的问题,那么就没有办法了。好的,这实际上解决了我的问题,但正如我所说,“d:Source”告诉我“Source不可附加到ResourceDictionary类型的元素”。当然,我添加了xmlns。我正在使用VS2013。我知道有什么问题吗?
<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>