Xamarin Forms-NavigationPage.TitleView在iOS上不起作用

时间:2019-10-17 14:38:21

标签: xamarin xamarin.forms xamarin.ios

我有一个简单的(文件->新项目)XF应用,该应用不会显示iOS的NavigationPage.TitleView内容。 Android运行正常。我在做什么错了?

可以找到源代码here

XAML

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="SampleApp.MainPage">

    <NavigationPage.TitleView>
        <Grid BackgroundColor="Blue">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Label Grid.Row="0" Grid.Column="0"
                   TextColor="Black"
                   Text="Page Title" />
        </Grid>
    </NavigationPage.TitleView>

    <StackLayout>
        <!-- Place new controls here -->
        <Label Text="Welcome to Xamarin.Forms!" 
               HorizontalOptions="Center"
               VerticalOptions="CenterAndExpand" />
    </StackLayout>

</ContentPage>

这是输出...

enter image description here

我还尝试了StackLayout,结果相同...

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="SampleApp.MainPage">

    <NavigationPage.TitleView>
        <StackLayout BackgroundColor="Blue" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
            <Label Grid.Row="0" Grid.Column="0"
                   TextColor="Black"
                   Text="Page Title" />
        </StackLayout>
    </NavigationPage.TitleView>

    <StackLayout>
        <!-- Place new controls here -->
        <Label Text="Welcome to Xamarin.Forms!" 
               HorizontalOptions="Center"
               VerticalOptions="CenterAndExpand" />
    </StackLayout>

</ContentPage>

1 个答案:

答案 0 :(得分:1)

如评论中的Jason所述, NavigationPage.TitleViewNavigation BarNavigationPage的一部分。要自定义TitleView,您首先应创建一个NavigationPage

public App ()
{
  MainPage = new NavigationPage (new Page1());
}

然后在ContentPage中设置TitleView

<NavigationPage.TitleView>
    <Label Text="Page Title" />
</NavigationPage.TitleView>

引用:Hierarchical Navigation