【问题标题】:Load xaml ResourceDictionary from dll从 dll 加载 xaml ResourceDictionary
【发布时间】:2018-08-10 08:17:23
【问题描述】:

我已经为我们的应用程序团队制作了几个ResourceDictionaries,以用于他们未来的应用程序。我已将包含这些字典的类库项目的内容部署到 .dll 文件中,并且希望能够通过在新的 WPF 解决方案中添加对 .dll 文件的引用来使用字典,我希望在其中创建一个新的应用程序.

在我的示例中,类库名为“NWF_Class_Library.dll”,并与 MainWindow.xaml 文件保存在 Windows 资源管理器中的同一文件夹中。是否可以从其中检索资源字典?

我已经阅读了有关组织使用arrange their xaml 资源的最佳方式的文章,所以它似乎必须是可能的,但我发现的只是使用“//pack:application:...”语法的方法在与 wpf 应用程序相同的解决方案中引用 xaml。这是一段 sn-p 代码,Source 为空白,因为我写的任何东西都没有用!

我们曾希望我们可以将标准配置以及我们更正常的有用方法等添加到可以与应用程序一起部署的文件中。

<Window x:Class="dll_ref_included.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <ResourceDictionary Source=""/>
    </Window.Resources>
    <Grid>
        <Button Style="{StaticResource myButton}">This</Button>
    </Grid>
</Window>

【问题讨论】:

  • 可以使用 "//pack:application:..." 语法。您可以提供您希望使用的强程序集名称和资源名称。据我所知,这个程序集必须加载到您当前的 AppDomain 中(添加到您的项目的引用中,或手动添加)。

标签: c# wpf xaml dll resourcedictionary


【解决方案1】:

试试这个:

<ResourceDictionary Source="pack://application:,,,/NWF_Class_Library;component/Dictionary1.xaml"/>

...其中“NWF_Class_Library”是引用程序集的名称,“Dictionary1.xaml”是此项目中定义的ResourceDictionary 的名称。

您可以参考documentation 了解有关包 URI 以及如何使用它们的更多信息。

【讨论】:

  • 难以置信!我已经尝试过这种语法并在设计器中出现错误An error occurred while finding the resource dictionary "pack://application:,,,/NWF_Class_Library;component/WPF Resources/Buttons.xaml". 但是当我忽略错误并运行它时(我以前没有这样做过),我看到了一个完美的按钮!什么鬼?!
  • 设计师有点bug,无法处理所有场景。重要的是运行时行为。您可能想要完全禁用设计器:stackoverflow.com/questions/10481645/disable-xaml-ui-designer
  • 伙计,为此浪费了太多时间!我即将开始学习 xaml 并且非常确定我的语法是正确的;只是在出错时从未真正按下 F5 !下次我会知道用更多的调味料来弥补设计师的错误——我会看看 Blend 是否会做同样的事情;在设计它们的那一刻不能看到形式会很遗憾......
  • Microsoft Blend 正确显示预览 - 我会使用它。男人!这么多浪费!谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多