我正在使用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
)合併它們,異常消失。
但是,我仍然想知道爲什麼會發生這種情況。
我覺得TelerikCustomizations.xaml是無法找到RadRibbonViewStyle在支持算法FMP = 「{StaticResource的獲取ResourceKey = RadRibbonViewStyle}」 – ethicallogics
啊,但是爲什麼呢?主題/ Windows8/Telerik.Windows.Controls.RibbonView.xaml在TelerikCustomizations.xaml之前被合併。 – cguedel
是不是缺少來自第一個資源字典源的'Themes'? –