2016-02-22 24 views
2

我有一個嘮叨的問題與我的App.Resources MergedDictionary。每次使用另一個程序集的源和XML名稱空間添加新字典時,都會產生錯誤,並且無法構建我的程序。WPF MergedDictionaries - 值不能爲空 - 參數名稱:項目

錯誤出現在App.xaml中,ResourceDictionary帶下劃線,消息Value Cannot be Null. Parameter Name: item。這絕對沒有意義,因爲我之前完成了完全相同的事情。唯一的區別是,這一次,我引用了另一個程序集(c#類庫)的名稱空間。這裏是App.xaml文件:

<Application x:Class="Quiz.Client.App" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:local="clr-namespace:Quiz.Client" 
      StartupUri="MainWindow.xaml"> 
    <Application.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="Resources/Styles.xaml" /> 
       <ResourceDictionary Source="Resources/Templates.xaml"/> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </Application.Resources> 
</Application> 

下面是Styles.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="clr-namespace:Quiz.Client"> 

    <Style x:Key="StrongFont" 
      TargetType="ContentControl"> 
     <Setter Property="FontSize" 
       Value="20" /> 
     <Setter Property="FontWeight" 
       Value="ExtraBold" /> 
     <Setter Property="Foreground" 
       Value="DarkRed" /> 
    </Style> 


</ResourceDictionary> 

這裏是Templates.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="clr-namespace:Quiz.Client" 
        xmlns:domain="clr-namespace:Quiz.Core.Domain;assembly=Quiz.Core" 
        xmlns:common="clr-namespace:Quiz.Client.Common"> 

    <DataTemplate x:Key="ArithmeticQuestionTemplate" 
        DataType="domain:ArithmeticQuestion"> 

    </DataTemplate> 

    <common:QuestionTemplateSelector x:Key="QuestionTemplateSelector" 
            ArithmeticTemplate="{StaticResource ArithmeticQuestionTemplate}" /> 

</ResourceDictionary> 

究竟發生了什麼事?是否引入了軟件突破性更改?我完全失去了。

+0

你有這個好運嗎? –

回答

0

不知道爲什麼你的方法不工作,但它不是我通常自己導入資源的方式。通常我會添加的ResourceDictionary到我的庫項目,把所有的圖書館資源在使用包語法其導入到我的主要項目:

<Application.Resources> 
    <ResourceDictionary> 

     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="pack://application:,,,/MyLibrary;component/LibraryResources.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 

     <!-- main project resources go here --> 

    </ResourceDictionary> 

總的來說我認爲這是一個更好的架構呢因爲每次有一個子庫資源發生變化時,不需要重新編譯主項目。如果您決定切換到MEF,它也會使以後更容易,因爲您的庫資源現在封裝在單個ResourceDictionary對象中,您可以輕鬆地將它們封裝爲[Export]

+0

感謝您的回答。我相信這是一個由未知事物引起的Visual Studio錯誤。也許我的插件之一,誰知道。解決此問題的唯一方法是重新啓動Visual Studio並構建項目。從另一個程序集添加新名稱空間時,問題似乎開始了。直到Visual Studio重新啓動後,該項目才能建立。非常奇怪,以前從未見過類似的東西。 – CanadaIT

相關問題