gpt4 book ai didi

wpf - 运行时找不到基于之前定义的 StaticResource 的样式

转载 作者:行者123 更新时间:2023-12-04 14:59:53 25 4
gpt4 key购买 nike

我正在将 Telerik 的 RadControls 用于带有隐式样式的 WPF。以下样式定义于 Themes/Windows8/Telerik.Windows.Controls.RibbonView.xaml :

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

我自己的样式和 Telerik 默认的样式在程序集中像这样合并 Lib.Windows.Controls在文件夹 Themes :
<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 )中,异常消失了。

但是,我仍然想知道为什么会发生这种情况。

最佳答案

您需要在 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>

现在您可以在任何地方使用/合并这本词典。

您需要这样做,因为 StaticResource不在“姐妹”之间工作 MergedDictionaries因此您不能引用在同一级别合并的资源,因为 StaticResource只向后看 直销 parent :

来自 MSDN :

XAML resource references within a particular resource dictionary must reference a resource that has already been defined with a key, and that resource must appear lexically before the resource reference. Forward references cannot be resolved by a XAML resource reference



但是当使用 MergedDictionaries :

In the resource-lookup sequence, a MergedDictionaries dictionary is checked only after a check of all the keyed resources of the ResourceDictionary that declared MergedDictionaries.

关于wpf - 运行时找不到基于之前定义的 StaticResource 的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18337002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com