【问题标题】:Avoid to refer styles in all pages - Xamarin.Forms避免在所有页面中引用样式 - Xamarin.Forms
【发布时间】:2017-04-03 12:19:35
【问题描述】:

我正在开发用于移动跨平台的 Xamarin Forms 应用程序。 我找到了如何以这种方式将样式应用于我的页面和控件:

Styles/HeaderStyle.xaml

<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    x:Class="App.HeaderStyle">
    <Style x:Key="Header" TargetType="StackLayout">
        <Setter Property="Orientation" Value="Horizontal"/>
        <Setter Property="Padding" Value="5"/>
        <Setter Property="BackgroundColor" Value="Green"/>
    </Style>
</ResourceDictionary>

Views/Page.xaml

<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    x:Class="App.HomePage"
    xmlns:local="clr-namespace:App;"
    NavigationPage.HasNavigationBar="false">
    <ContentPage.Resources>
        <ResourceDictionary MergedWith="local:HeaderStyle">
        </ResourceDictionary>
    </ContentPage.Resources>
    <!-- Some other page content -->
</ContentPage>

我对这个实现有些怀疑: - 我不知道如何添加多个样式文件 - 我必须将样式文件引用添加到所有页面

我尝试以某种方式在 App.xaml 中添加引用

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="theme:Resources1"/>
    <ResourceDictionary Source="theme:Resources2"/>
</ResourceDictionary.MergedDictionaries>

但没有成功。

【问题讨论】:

    标签: xamarin xamarin.forms resourcedictionary mergeddictionaries


    【解决方案1】:

    如果您有随处使用的样式,可以将它们放在global style 中。

    在您的App.xaml 中,您可以像现在一样定义ResourceDictionary。例如:

    <Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Styles.App">
        <Application.Resources>
            <ResourceDictionary>
                <Style x:Key="buttonStyle" TargetType="Button">
                    <Setter Property="HorizontalOptions" Value="Center" />
                    <Setter Property="VerticalOptions" Value="CenterAndExpand" />
                    <Setter Property="BorderColor" Value="Lime" />
                    <Setter Property="BorderRadius" Value="5" />
                    <Setter Property="BorderWidth" Value="5" />
                    <Setter Property="WidthRequest" Value="200" />
                    <Setter Property="TextColor" Value="Teal" />
                </Style>
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    

    然后在您的页面中,您应该能够引用它们而无需声明字典的合并,如下所示:

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Styles.ApplicationStylesPage" Title="Application" Icon="xaml.png">
        <ContentPage.Content>
            <StackLayout Padding="0,20,0,0">
                <Button Text="These buttons" Style="{StaticResource buttonStyle}" />
                <Button Text="are demonstrating" Style="{StaticResource buttonStyle}" />
                <Button Text="application style overrides" Style="{StaticResource buttonStyle}" />
            </StackLayout>
        </ContentPage.Content>
    </ContentPage>
    

    如果要覆盖样式,可以通过在该页面中声明样式来实现,层次结构中较低的样式优先于较高的样式。 您还可以选择不在样式上添加 x:Key 属性以使其隐含。这样您就不必在控件上声明Style 属性。

    【讨论】:

    • 我是否也可以将样式拆分到多个文件中并在 App.xaml 中引用这些文件?
    • 我认为目前没有任何支持。
    猜你喜欢
    • 2013-02-03
    • 2020-08-20
    • 2015-11-13
    • 1970-01-01
    • 2020-02-12
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 2012-11-11
    相关资源
    最近更新 更多