改变Xamarin主题,每个客户方式的风格

时间:2017-07-08 05:56:11

标签: xamarin xamarin.forms

如果您使用xamarin进行移动应用构建,那么您会使用什么方法,这个应用就像一个香草应用。

每个客户都应该能够将自己的风格应用于标签等。主题

基本上所有客户的所有视图/页面都是相同的,但每个客户的每页样式都会改变。

如果这不是一个移动应用程序,我会在一个文件夹中插件,并根据客户端我会删除pluginFolder中的相关插件,例如:pluginClientA等..这将在运行时使用mef发现。

任何建议

1 个答案:

答案 0 :(得分:0)

将样式设置为动态资源,如https://developer.xamarin.com/guides/xamarin-forms/user-interface/styles/dynamic/

所示

我只有2个主题,所以我在xaml中指定了它们

<ResourceDictionary>
        <Color x:Key="Primary">#337ab7</Color>
        <Color x:Key="Accent">#96d1ff</Color>
        <Color x:Key="BackgroundColor">#C0C0C0</Color>
        <Color x:Key="TextColor">White</Color>

        <!-- Buttons -->
        <Style x:Key="buttonStyle" TargetType="Button">
            <Setter Property="BorderRadius" Value="4" />
            <Setter Property="FontSize" Value="48" />
            <Setter Property="FontAttributes" Value="Bold" ></Setter>
                <Setter Property="HeightRequest">
                <Setter.Value>
                    <OnPlatform x:TypeArguments="x:Double" iOS="80" Android="80" />
                </Setter.Value>
            </Setter>
            <Setter Property="WidthRequest">
                <Setter.Value>
                    <OnPlatform x:TypeArguments="x:Double" iOS="150" Android="150" />
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="btnPrimary" TargetType="Button" BasedOn="{StaticResource buttonStyle}">
            <Setter Property="TextColor" Value="{DynamicResource TextColor}" />
            <Setter Property="BackgroundColor" Value="{DynamicResource Primary}" />
        </Style>

        <!--Light Theme-->
        <Color x:Key="Primary-Light">#2196F3</Color>
        <Color x:Key="BackgroundColor-Light">#FAFAFA</Color>
        <Color x:Key="TextColor-Light">White</Color>

        <!--Dark Theme-->
        <Color x:Key="Primary-Dark">#1976D2</Color>
        <Color x:Key="BackgroundColor-Dark">#C0C0C0</Color>
        <Color x:Key="TextColor-Dark">Black</Color>

    </ResourceDictionary>

因此,在主题更改事件中,您可以更改指定的颜色

Resources ["Primary"] = Resources ["Primary-Dark"];