【问题标题】:WPF Resource access from a different assembly if no App.xaml如果没有 App.xaml,则来自不同程序集的 WPF 资源访问
【发布时间】:2011-07-08 20:40:48
【问题描述】:

我正在为现有的 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 发生了
Message="MyResourceDLL;组件/字典/styles.xaml' 值不能分配给属性 对象的“来源” 'System.Windows.ResourceDictionary'。 找不到资源 'ems.wpf.resources;component/dictionaries/styles.xaml'。 对象错误 'System.Windows.ResourceDictionary' 在 标记文件 'SARMaster.Maryln.EphemerisLib;component/getephemeriscontrol.xaml' 第 9 行位置 37。”

我有什么方法可以加载主项目未引用的相关 DLL?

【问题讨论】:

  • 不明白。为什么不引用包含资源字典的程序集?我认为你需要进一步解释你的情况。
  • 你试过this吗?
  • Kent,WPF 类库正在引用包含资源字典的程序集。我需要告诉托管 MFC 应用程序将所有托管 DLL 复制到其输出目录或其他内容。与 WPF 宿主不同,将此程序集引用添加到 MFC 项目不会将其复制到其输出目录。如果我自己手动复制文件,一切都很好。
  • dowhilefor,你提供的链接很有帮助,谢谢。我认为这主要是构建路径的问题。如果我把我的程序集放到 MFC 主机的输出目录中,一切都很好。
  • ... 或者它可能是被引用的程序集仅在 XAML 中使用,而不是在入口项目中没有引用的代码中,它没有被运行时加载。

标签: wpf resources embedding


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    修复此错误后,我需要执行以下步骤才能使某些东西 100% 正常工作:

    这是我的项目:

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

    这样做:

    1. 在我的 MainProject 中添加我的 StyleProject 的引用 (see here)
    2. 在我的 MainProject 中创建一个名为 MyStyle.xamlResourceDictionary
    3. this answer 之后我的StyleProject 的不同字典添加到MyStyle.xaml
    4. 使用以下代码将 MyStyle.xaml 添加到 App.xaml

    代码:

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

    【讨论】:

      猜你喜欢
      • 2011-08-17
      • 1970-01-01
      • 2011-05-27
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多