Mvvmcross Xamarin表单导航页面样式不起作用

时间:2019-02-05 15:36:08

标签: xamarin xamarin.forms mvvmcross

我正在尝试使用Mvvmcross在xamarin Forms应用程序上设置导航页面的样式。

在“添加” mmvcross之前,我已经在 App.xaml 中定义了样式,如下所示。

<Style TargetType="NavigationPage">
    <Setter Property="BarBackgroundColor" Value="Black" />
    <Setter Property="BarTextColor" Value="White" />
</Style>

成功了。我得到了我定义的背景颜色和文本颜色。然后我们“移动”到Mvvmcross-我们添加了所有必需的后端代码,将所有页面从ContentPage更改为MvxContentPage等,并且...导航页面的背景色在Android上停止工作(我没有尝试过在iOS上)。如果我更改了{strong> App.xaml 中的BarTextColor,则颜色也会更改。如果我更改BackgroundColor,它也可以工作-所有应用程序的背景色都已更改。但是无论我应用于BarBackgroundColor的值是多少,它仍然是白色的。

我的 App.xaml 如下:

<?xml version="1.0" encoding="utf-8" ?>
<core:MvxFormsApplication xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:views="clr-namespace:MvvmCross.Forms.Views;assembly=MvvmCross.Forms"
             xmlns:viewModels="clr-namespace:MvvmCross.ViewModels;assembly=MvvmCross"
             xmlns:core="clr-namespace:MvvmCross.Forms.Core;assembly=MvvmCross.Forms"
             xmlns:attributes="clr-namespace:MvvmCross.Forms.Presenters.Attributes;assembly=MvvmCross.Forms"
             x:Class="ShowMeTheLogs.Core.FormsApp">
    <core:MvxFormsApplication.Resources>
        <!--Global Styles-->
        <Color x:Key="ColorPrimary">#98C340</Color>
        <Color x:Key="ColorError">#CF1212</Color>
        <Color x:Key="ColorWarning">#E4AD17</Color>
        <Color x:Key="ColorInfo">#1283CF</Color>
        <Color x:Key="ColorDebug">#989898</Color>
        <Style TargetType="NavigationPage">
            <Setter Property="BarBackgroundColor" Value="{StaticResource ColorPrimary}" />
            <Setter Property="BarTextColor" Value="White" />
        </Style>

        <Style x:Key="SubmitButton" TargetType="Button">
            <Setter Property="BackgroundColor" Value="{StaticResource ColorPrimary}" />
            <Setter Property="TextColor" Value="White" />
        </Style>

        <Style x:Key="circleButton" TargetType="Button">
            <Setter Property="BackgroundColor" Value="{StaticResource ColorPrimary}" />
            <Setter Property="BorderRadius" Value="50" />
            <Setter Property="WidthRequest" Value="100" />
            <Setter Property="HeightRequest" Value="100" />
            <Setter Property="TextColor" Value="White" />
            <Setter Property="FontSize" Value="50" />
        </Style>

        <Style x:Key="smallSquareButton" TargetType="Button">
            <Setter Property="BackgroundColor" Value="{StaticResource ColorPrimary}" />
            <Setter Property="WidthRequest" Value="100" />
            <Setter Property="HeightRequest" Value="100" />
            <Setter Property="TextColor" Value="White" />
            <Setter Property="FontSize" Value="Micro" />
            <Setter Property="HorizontalOptions" Value="Center" />
            <Setter Property="VerticalOptions" Value="Center" />
        </Style>
    </core:MvxFormsApplication.Resources>
</core:MvxFormsApplication>

我的 App.xalm.cs 是最简单的:

public partial class FormsApp: MvxFormsApplication
    {
        public App()
        {
            InitializeComponent();
        }
    }

和最简单的视图:

<?xml version="1.0" encoding="utf-8" ?>
<views:MvxContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:views="clr-namespace:MvvmCross.Forms.Views;assembly=MvvmCross.Forms"
             x:TypeArguments="viewModels:WebAppListViewModel"
             x:Class="ShowMeTheLogs.Core.Views.WebAppListPage"
             xmlns:bindings="clr-namespace:MvvmCross.Forms.Bindings;assembly=MvvmCross.Forms"
             xmlns:viewModels="clr-namespace:ShowMeTheLogs.Core.ViewModels;assembly=ShowMeTheLogs.Core"
             Title="Sample title"
             NavigationPage.HasBackButton="False">
    <ContentPage.ToolbarItems>
        <ToolbarItem x:Name="AddNewApp" Order="Secondary" Text="Dodaj aplikację" Priority="0" Command="{bindings:MvxBind NewWebAppCommand}"/>
    </ContentPage.ToolbarItems>

    <RelativeLayout Padding="0" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
       <Non relevant="Code" />
    </RelativeLayout>

</views:MvxContentPage>

我真的不知道为什么它不起作用。 Androud代码中唯一的更改是将class RootActivity: CompatActivity更改为class RootActivity: MvxFormsAppCompatActivity

有人曾经打过这个问题吗? 软件包是从NuGet获得的 -MvvmCross(和MvvmCross.Forms)6.2.2版 -Xamarin版本28.0.0 -Xamarin.Forms版本3.4.0

1 个答案:

答案 0 :(得分:1)

在您的MainActivity OnCreate方法中,确保设置工具栏资源的两行位于base.OnCreate()调用之前

protected override void OnCreate(Bundle bundle)
{
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;
    base.OnCreate(bundle);
}

https://github.com/MvvmCross/MvvmCross/issues/2301