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

发布于 2024-11-19 07:25:00 字数 1053 浏览 4 评论 0原文

我正在为现有 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” 无法将值分配给属性 对象的“来源” '系统.Windows.资源字典'。 无法找到资源 'ems.wpf.resources;组件/字典/styles.xaml'。 对象错误 'System.Windows.ResourceDictionary' 中 标记文件 'SARMaster.Maryln.EphemerisLib;组件/getephemeriscontrol.xaml' 第 9 行位置 37。”

我有办法加载主项目未引用的相关 DLL 吗?

I have am creating a WPF extension to an existing Win32 MFC client application. Within a UserControl located in my WPF class library, I am merging libraries as follows:

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

I also tried

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

In either case, I get the following XamlParseException:

System.Windows.Markup.XamlParseException
occurred
Message="MyResourceDLL;Component/dictionaries/styles.xaml'
value cannot be assigned to property
'Source' of object
'System.Windows.ResourceDictionary'.
Cannot locate resource
'ems.wpf.resources;component/dictionaries/styles.xaml'.
Error at object
'System.Windows.ResourceDictionary' in
markup file
'SARMaster.Maryln.EphemerisLib;component/getephemeriscontrol.xaml'
Line 9 Position 37."

I there a way I can load a relative DLL that is not referenced by main project?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巡山小妖精 2024-11-26 07:25:00

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

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

I've been looking at the same issue recently. When compiling a Win32 CLR project the dependencies of the assemblies referenced by the MFC project aren't copied, so I simply set up a post-build event to copy the appropriate assemblies to the $(TargetDir).

Not ideal, but I believe it's by design.

毅然前行 2024-11-26 07:25:00

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

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

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

这里是我的项目:

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

为此:

  1. 在我的 MainProject 中添加我的 StyleProject 的引用 (参见此处
  2. 创建一个名为的 ResourceDictionary MainProject 中的 MyStyle.xaml
  3. 此答案MyStyle.xaml
  4. 添加使用以下代码将 MyStyle.xaml 复制到 App.xaml 代码

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

I got the same problem and I found the solution. I needed to remove the Style of my ContentPresenter. This line was creating the XamlParseException:

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

And after fixing this error, I needed to do these steps to have something 100% working:

Here my projects:

  • StyleProject: the Class Library project that I want to use. It contains my styles
  • MainProject: the project that will use the styles

To do so:

  1. Add the reference of my StyleProject inside my MainProject (see here)
  2. Create a ResourceDictionary called MyStyle.xaml inside my MainProject
  3. Add the different dictionaries of my StyleProject following this answer to MyStyle.xaml
  4. Add MyStyle.xaml to the App.xaml using the following code

Code:

<ResourceDictionary Source="Resources/MyStyle.xaml"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文