WPF应用程序插件使用来自主机应用程序的资源

时间:2016-09-14 18:09:39

标签: c# wpf xaml plugins staticresource

我对this问题有类似的情况。但是,用户会在运行时利用来自主机应用程序的静态资源来了解他们如何使用插件。那是我的绊脚石。

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

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

<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

消息=已添加具有相同键的项目。脱颖而出。

非常感谢任何见解。提前谢谢!

1 个答案:

答案 0 :(得分:0)

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

这使我与此问题保持同一点:https://stackoverflow.com/questions/29749256/static-resources-in-wpf-in-pluggable-application