2012-08-09 54 views
4

我目前正在使用MVVMLight框架構建WP7應用程序。我想添加一個資源字典到我的app.xaml,但是當我做它失敗。下面是從App.xaml中引用Windows Phone 7中的合併資源字典失敗

<Application.Resources> 
    <!--Global View Model Locator--> 
    <vm:ViewModelLocator x:Key="Locator" 
         d:IsDataSource="True" /> 
    <!--Merged Resource Dictionaries--> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="View/StyleResourceDictionary.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

一個snipit因爲我使用的是有一個鍵ViewModelLocator,我得到一個錯誤警告我,我不能有和無鑰匙的資源組合。加入一鍵我的資源字典之後,它看起來如下:

​​

在資源字典我有鑰匙「TitleTemplate」風格。無論哪種情況,當我嘗試從我的某個視圖中引用資源字典時,它都會失敗。從我的觀點示例代碼波紋管:

<TextBlock Name="TB_ContactNameLabel" 
      Text="contact" 
      Style="{StaticResource TitleTemplate}"/> 

設計師馬上給我的錯誤「資源‘TitleTemplate’無法解析」。如果我引用資源字典的關鍵字(即:resourceDictionary),則不會拋出任何錯誤,但它不會顯式執行任何操作。最後,如果我將resourceDictionary直接添加到其資源中的頁面,而不是app.xaml,那麼一切正常。我不想將其添加到我打算使用的每個視圖中。我在這裏錯過了什麼嗎?

回答

8

您的應用程序資源,應該像下面:

<Application.Resources> 
    <!--Global View Model Locator--> 
    <!--Merged Resource Dictionaries--> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="View/StyleResourceDictionary.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
     <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" /> 
    </ResourceDictionary> 
</Application.Resources> 
+0

真棒,這完美地工作。但你能解釋爲什麼會出現這種情況嗎? – ferics2 2012-08-09 16:11:53

+0

@ ferics2:如果使用合併字典,資源字典必須是資源屬性定義的單根,並且必須在其中定義所有資源。這只是它的工作方式。 – Will 2012-08-09 17:50:17

+0

謝謝@威爾。是的,當添加另一個字典到現有的字典時(合併),則需要將其他資源放入字典中。 – 2012-08-09 22:18:49