【问题标题】:WPF application plugin using resources from host application使用来自主机应用程序的资源的 WPF 应用程序插件
【发布时间】:2016-09-14 18:09:39
【问题描述】:

我遇到了与this 问题类似的情况。但是,用户对他们如何能够让插件在运行时利用来自主机应用程序的静态资源感到困惑。这是我的绊脚石。

让我们称主机为“K”。目前我已经创建了一个名为 KResources.xaml 的资源字典,其中保存了应用程序的所有资源(app.xaml 只是合并了这个字典并称之为一天)。

在我的插件中,我正在创建一个视图 V,它需要使用 KResources 字典。这是我目前正在尝试的:

<UserControl.Resources>
 <ResourceDictionary>
  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/K;component/Resources/KResources.xaml" />
  </ResourceDictionary.MergedDictionaries>
 </ResourceDictionary>
</UserControl.Resources>

我也尝试过以下语法:

<ResourceDictionary 
    Source="pack://application:,,,/K;component/Resources/KResources.xaml" />

编译顺利,我的 V.xaml 文件可以识别资源,但是在创建此视图时在运行时出现异常:

System.Windows.Markup.XamlParseException occurred
HResult=-2146233087
LineNumber=19
LinePosition=8
Message='The invocation of the constructor on type 'K.UI.Infrastructure.CaliburnBootstrapper' that matches the specified binding constraints threw an exception.' Line number '19' and line position '8'.
Source=PresentationFramework
StackTrace:
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
InnerException: 
   HResult=-2147024809
   Message=An item with the same key has already been added.
   Source=mscorlib
   StackTrace:
        at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
        at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
        at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
        at Caliburn.Micro.AssemblySourceCache.<Install>b__4(Type t)
        at Caliburn.Micro.EnumerableExtensions.Apply[T](IEnumerable`1 enumerable, Action`1 action)
        at Caliburn.Micro.AssemblySourceCache.<Install>b__0(Object s, NotifyCollectionChangedEventArgs e)
        at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        at Caliburn.Micro.BindableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        at Caliburn.Micro.BindableCollection`1.<>c__DisplayClasse.<AddRange>b__d()
        at Caliburn.Micro.XamlPlatformProvider.OnUIThread(Action action)
        at Caliburn.Micro.BindableCollection`1.AddRange(IEnumerable`1 items)
        at Caliburn.Micro.BootstrapperBase.StartRuntime()
        at Caliburn.Micro.BootstrapperBase.Initialize()
        at K.UI.Infrastructure.CaliburnBootstrapper..ctor() in C:\Dev\K\source\K.UI\Infrastructure\CaliburnBootstrapper.cs:line 44

Message=An item with the same key has been added.这行很突出。

任何见解将不胜感激。提前致谢!

【问题讨论】:

  • 两个字典中是否有元素使用相同的键名?哦,第 19 行是什么?
  • 我认为第 19 行是指 WpfXamlLoader 类中的一行。参考:referencesource.microsoft.com/#PresentationFramework/src/… 我将对我的项目进行快速审核,以查找使用相同名称定义的键。感谢您的建议!我会在完成后发表评论。
  • 好的,我已经尽可能彻底地梳理了解决方案(向 Notepad++ 和 regexr.com 大声疾呼)。我发现了几个重复的键,但它们的作用域不应该发生冲突。我无论如何都删除了名称重复,但仍在与运行时相同的无用异常作斗争。

标签: c# wpf xaml plugins staticresource


【解决方案1】:

事实证明,完全省略合并字典代码会导致我需要的静态资源在运行时被正确定位。缺点是在设计时 IDE 会突出显示它们,就好像它们不存在一样。

这让我和这个问题在同一点上:https://stackoverflow.com/questions/29749256/static-resources-in-wpf-in-pluggable-application

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多