从ContentView访问App.xaml中的全局资源

时间:2020-06-20 06:04:26

标签: xamarin xamarin.forms xamarin.android xamarin.ios

我在App.xaml资源字典中定义了一种颜色。当我尝试在ContentView中引用它时,出现运行时异常:

找不到NavbarBackground键的

StaticResource

我需要在App.xaml中合并资源字典吗?

应用程式
<Application.Resources>
    <ResourceDictionary>
        <!--Global Styles-->
        ...
        <Color x:Key="NavbarBackground">Black</Color>
        ...
    </ResourceDictionary>
</Application.Resources>
CustomTitleView.xaml
<ContentView>
    ...
    <ContentView.Content>
         ...
                <ImageButton Source="search.png" HorizontalOptions="Center"
                    VerticalOptions="CenterAndExpand" BackgroundColor="{StaticResource NavbarBackground}" />
         ...
     </ContentView.Content>
</ContentView>

1 个答案:

答案 0 :(得分:-1)

如果使用单独的xaml文件定义资源,请使用ResourceDictionary。如果您是App.xaml中的设置值,请在Application.Resources中设置它们。请参阅文档here