在通过StaticResource not found for key
合并的Style
中使用ResourceDictionary
时,我不断收到运行时错误MergedDictionaries
。
<Application x:Class="MyApp.App"
xmlns:local="MyApp"
xmlns:assets="clr-namespace:MyApp.Assets">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<assets:Styles />
</ResourceDictionary.MergedDictionaries>
<Style ...>
<!-- other styles... -->
<ResourceDictionary x:Class="MyApp.Assets.Styles">
<Style x:Key="Subheading" TargetType="Frame">
<Setter Property="BackgroundColor" Value="DarkSlateGray"></Setter>
<Setter Property="HasShadow" Value="True"></Setter>
</Style>
</ResourceDictionary>
namespace MyApp.Assets
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Styles : ResourceDictionary
{
public Styles () { }
}
}
<Frame Style="{StaticResource Subheading}">
该应用正确编译-在运行时找不到Subheading
。如果我将<Style>
节点直接复制到App.xaml中,它将正常工作。谁能找到我不正确的语法?还是有一种在运行时查看Application.Resources的方法,这样我可以弄清楚它在哪里?
答案 0 :(得分:0)
您在InitializeComponent()
构造函数中缺少Styles
方法调用。这样做:
namespace MyApp.Assets
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Styles : ResourceDictionary
{
public Styles()
{
InitializeComponent();
}
}
}