2016-09-14 117 views
0

我有和this問題類似的情況。但是,這些用戶琢磨了他們如何讓插件在運行時利用來自宿主應用程序的靜態資源。那是我的絆腳石。WPF應用程序插件使用來自主機應用程序的資源

讓我們打電話給主機「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 

消息=具有相同鍵的項已被添加。脫穎而出。

任何有識之士將不勝感激。提前致謝!

+0

你有兩個字典使用相同的密鑰名稱的元素?哦,第19行是什麼? –

+0

我認爲第19行是指WpfXamlLoader類中的一行。參考:http://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Markup/WpfXamlLoader.cs 我會做一個快速審計我的項目爲同名的定義鍵。感謝您的建議!我完成後會回覆評論。 – 780farva

+0

好吧,我已經儘可能徹底地梳理了解決方案(對於記事本++和regexr.com來說,我認爲是rad)。我找到了一對重複的鍵,但它們的範圍是它們不應該發生碰撞。無論如何,我刪除了名稱重複,但仍然在運行時與之相同,無用的異常。 – 780farva

回答

相關問題