2013-08-20 31 views
6

我正在使用Telerik的RadControls WPF的隱式樣式。下面的風格在Themes/Windows8/Telerik.Windows.Controls.RibbonView.xaml定義:基於先前定義的StaticResource的樣式在運行時沒有找到

<Style TargetType="telerikRibbonView:RadRibbonView" x:Key="RadRibbonViewStyle"> 
... 
</Style> 

我自己的風格和Telerik的默認的獲取文件夾Themes在合併這樣的組件Lib.Windows.Controls

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="Windows8/Telerik.Windows.Controls.RibbonView.xaml" />   
     <ResourceDictionary Source="MyTheme/TelerikCustomizations.xaml" /> 

     <ResourceDictionary> 
      <!-- avoid optimization --> 
      <Style TargetType="{x:Type Rectangle}" /> 
     </ResourceDictionary> 
    </ResourceDictionary.MergedDictionaries> 
</ResourceDictionary> 

而在TelerikCustomizations.xaml我定義以下(空,用於測試目的)風格:

<Style x:Key="MyThemeRadRibbonViewStyle" TargetType="{x:Type telerik:RadRibbonView}" BasedOn="{StaticResource ResourceKey=RadRibbonViewStyle}" /> 

這導致運行時出現以下異常:

'Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.' Line number '4' and line position '42'. {"Cannot find resource named 'RadRibbonViewStyle'. Resource names are case sensitive."}

害得我在MyView.xaml.cs以下調試語句:

public ShellView() 
{ 
    var baseStyle = FindResource("RadRibbonViewStyle"); 
    var inherited = FindResource("MyThemeRadRibbonViewStyle"); 
    InitializeComponent(); 
} 

現在的事情是:唯一的例外是在第二FindResource調用拋出。具有完全相同的信息。然而,RadRibbonViewStyle顯然可以在構造函數的第一行找到。

如果重要,合併的字典實際上是在App.xaml中第二次合併。

我敢肯定我錯過了一些明顯的東西,但我無法弄清楚什麼。

的App.xaml

<Application x:Class="TestClient.App" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    StartupUri="Views/ShellView.xaml"> 
    <Application.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="pack://application:,,,/Lib.Windows.Controls;component/Themes/MyTheme.xaml" /> 

       <ResourceDictionary> 
        <!-- added to avoid optimization --> 
        <Style TargetType="{x:Type Rectangle}" /> 
       </ResourceDictionary> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </Application.Resources> 
</Application> 

App.xaml.cs不會覆蓋的構造。實際上它什麼都不做。

更新

如果我在合併的TelerikCustomizations.xaml Telerik的字典,而不是在另一個字典(MyTheme.xaml)合併它們,異常消失。

但是,我仍然想知道爲什麼會發生這種情況。

+0

我覺得TelerikCustomizations.xaml是無法找到RadRibbonViewStyle在支持算法FMP = 「{StaticResource的獲取ResourceKey = RadRibbonViewStyle}」 – ethicallogics

+0

啊,但是爲什麼呢?主題/ Windows8/Telerik.Windows.Controls.RibbonView.xaml在TelerikCustomizations.xaml之前被合併。 – cguedel

+0

是不是缺少來自第一個資源字典源的'Themes'? –

回答

6

您需要在Windows8/Telerik.Windows.Controls.RibbonView.xaml合併在MyTheme/TelerikCustomizations.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="Windows8/Telerik.Windows.Controls.RibbonView.xaml" /> 
     <ResourceDictionary> 
      <Style x:Key="MyThemeRadRibbonViewStyle" TargetType="{x:Type telerik:RadRibbonView}" BasedOn="{StaticResource ResourceKey=RadRibbonViewStyle}" /> 
     </ResourceDictionary> 
    </ResourceDictionary.MergedDictionaries> 
</ResourceDictionary> 

現在你可以使用/無論你想合併這本詞典。

你需要這樣做,因爲StaticResourceMergedDictionaries之間的「姐姐」的工作,所以你不能引用這是在同一水平上合併的資源,因爲StaticResource看起來只有倒退到直接父母:

MSDN

特定資源字典中的XAML資源引用必須 參考已經被用密鑰定義的資源,並 該資源必須在詞源上出現在資源引用之前。 轉發引用,不能使用MergedDictionaries在由XAML資源引用

但解決:

在資源查找序列,MergedDictionaries字典 所有的的檢查後,只檢查聲明MergedDictionaries的 ResourceDictionary的鍵控資源。

+0

因此理論上,如果在'MyTheme.xaml'之前在App.xaml中包含'Windows8/Telerik.Windows.Controls.RibbonView.xaml',但我不是當然可以。從文檔中不清楚'StaticResource'是不適用於直接的「姐妹詞典」還是它不適用於「姐妹詞典分支」之間...... – nemesv

+0

非常感謝,這解釋了很多;) – cguedel

相關問題