找不到MergedDictionaries中的样式

时间:2018-11-20 07:15:24

标签: c# xaml xamarin.forms

在通过StaticResource not found for key合并的Style中使用ResourceDictionary时,我不断收到运行时错误MergedDictionaries

App.xaml

   <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... -->

Styles.xaml

<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>

Styles.xaml.cs

namespace MyApp.Assets
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Styles : ResourceDictionary
    {
        public Styles () { }
    }
}

Page.xaml

        <Frame Style="{StaticResource Subheading}">

该应用正确编译-在运行时找不到Subheading。如果我将<Style>节点直接复制到App.xaml中,它将正常工作。谁能找到我不正确的语法?还是有一种在运行时查看Application.Resources的方法,这样我可以弄清楚它在哪里?

1 个答案:

答案 0 :(得分:0)

您在InitializeComponent()构造函数中缺少Styles方法调用。这样做:

namespace MyApp.Assets
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Styles : ResourceDictionary
    {
        public Styles()
        {
            InitializeComponent();
        }
    }
}