如果没有App.xaml,则从不同的程序集访问WPF资源

时间:2011-07-08 20:40:48

标签: wpf resources embedding

我正在为现有的Win32 MFC客户端应用程序创建WPF扩展。在我的WPF类库中的UserControl中,我按如下方式合并库:

 <ResourceDictionary.MergedDictionaries>                
                <ResourceDictionary Source="MyResourceDLL;Component/dictionaries/styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>

我也试过

   <ResourceDictionary.MergedDictionaries>                
                    <ResourceDictionary Source="pack://application:,,,/MyResourceDLL;Component/dictionaries/styles.xaml"/>
                </ResourceDictionary.MergedDictionaries>

在任何一种情况下,我都会得到以下XamlParseException:

  

System.Windows.Markup.XamlParseException   发生
  消息=“MyResourceDLL;成分/字典/ styles.xaml”   值不能分配给属性   对象的“来源”   'System.Windows.ResourceDictionary'。   找不到资源   'ems.wpf.resources;组件/字典/ styles.xaml'。   对象出错   'System.Windows.ResourceDictionary'中   标记文件   'SARMaster.Maryln.EphemerisLib;组件/ getephemeriscontrol.xaml'   9号线位置37。“

我有一种方法可以加载主项目未引用的相对DLL吗?

2 个答案:

答案 0 :(得分:0)

我最近一直在看同样的问题。编译Win32 CLR项目时,不会复制MFC项目引用的程序集的依赖项,因此我只需设置一个构建后事件,将相应的程序集复制到$(TargetDir)。

不理想,但我相信这是设计的。

答案 1 :(得分:0)

我遇到了同样的问题,我找到了解决方案。我需要删除ContentPresenter的样式。这一行创建了XamlParseException:

<ContentPresenter.Resources>
    <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource TextStyle}"/>
</ContentPresenter.Resources>

修复此错误后,我需要执行以下步骤才能完成100%的工作:

这是我的项目:

  • StyleProject :我想要使用的Class Library项目。它包含我的样式
  • MainProject :将使用样式的项目

这样做:

  1. 在我的 MainProject see here
  2. 中添加 StyleProject 的引用
  3. MainProject
  4. 中创建名为 MyStyle.xaml ResourceDictionary
  5. this answer后面的 StyleProject 的不同词典添加到 MyStyle.xaml
  6. 使用以下代码将 MyStyle.xaml 添加到 App.xaml
  7. 代码:

    <ResourceDictionary Source="Resources/MyStyle.xaml"/>