【问题标题】:Duplicating tool bar on xamrin forms在 xamarin 表单中复制工具栏
【发布时间】:2018-06-09 22:37:31
【问题描述】:

我正在使用 Xamrian 表单我遇到一个问题,当我从登录表单调用我的主页时,它会复制工具栏。

在我的 app.xml 中,我正在唱以下内容

public static Page GetMainPage()
{
        return new NavigationPage(new MainPage());
        //return new ExamplePage ();
}

在有人说这是一个最小的例子之前,我不想显示我所有的登录代码 obv 原因。

 void OnLoginButtonClicked(object sender, EventArgs e)
 {
        var isValid = true;
        if (isValid)
        {
             Application.Current.MainPage = App.GetMainPage();               

        }
        else
        {
            messageLabel.Text = "Login failed";
            passwordEntry.Text = string.Empty;
        }
    }

Login.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="DeliveryDriver.Views.LoginPage">
    <ContentPage.ToolbarItems>
        <ToolbarItem Text="Sign Up"  />
    </ContentPage.ToolbarItems>
    <ContentPage.Content>
        <StackLayout VerticalOptions="StartAndExpand">
            <Label Text="Username" />
            <Entry x:Name="usernameEntry" Placeholder="username" />
            <Label Text="Password" />
            <Entry x:Name="passwordEntry" IsPassword="true" />
            <Button Text="Login"  Clicked="OnLoginButtonClicked"/>
            <Label x:Name="messageLabel" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

我的主页 xaml。

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:views="clr-namespace:DeliveryDriver.Views"
            x:Class="DeliveryDriver.Views.MainPage">
    <TabbedPage.Children>
        <NavigationPage Title="Jobs">
            <NavigationPage.Icon>
                <OnPlatform x:TypeArguments="FileImageSource">
                    <On Platform="iOS" Value="tab_feed.png"/>
                </OnPlatform>
            </NavigationPage.Icon>
            <x:Arguments>
                <views:JobsPage />
            </x:Arguments>
        </NavigationPage>

        <NavigationPage Title="Items Test">
            <NavigationPage.Icon>
                <OnPlatform x:TypeArguments="FileImageSource">
                    <On Platform="iOS" Value="tab_feed.png"/>
                </OnPlatform>
            </NavigationPage.Icon>
            <x:Arguments>
                <views:ItemsPage />
            </x:Arguments>
        </NavigationPage>

        <NavigationPage Title="About">
            <NavigationPage.Icon>
                <OnPlatform x:TypeArguments="FileImageSource">
                    <On Platform="iOS" Value="tab_about.png"/>
                </OnPlatform>
            </NavigationPage.Icon>
            <x:Arguments>
                <views:AboutPage />
            </x:Arguments>
        </NavigationPage>
    </TabbedPage.Children>
</TabbedPage>

按照建议,我将 app.cs 调用更改为登录页面,如下所示。

 public App ()
 {
    InitializeComponent();
    MainPage = new NavigationPage(new LoginPage());
 }

您可以清楚地看到下图中内容工具栏重复的问题。

【问题讨论】:

    标签: c# xamarin.forms


    【解决方案1】:

    您将 NavigationPages 嵌套在导航堆栈中,这不是您想要做的。 GetMainPage() 返回一个带有 MainPage 子级的 NavigationPage。 MainPage 本身是 TabbedPage,其每个子项都是 NavigationPage。 NavigationPage 的每个可见层都会显示一个工具栏,这就是您看到它的原因。

    使用 TabbedPage 的最佳实践是将其用作导航堆栈的根,并将 NavigationPage 实例(或 ContentPage 实例)作为其子级。因此,只需这样做来清理导航堆栈,额外的工具栏就会消失:

    public static Page GetMainPage()
    {
            return new MainPage();
    }
    

    【讨论】:

    • 我发现,经过几个小时的测试,lol 是他们对于表单 3.0 的任何导航抽屉
    猜你喜欢
    • 1970-01-01
    • 2018-09-13
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    相关资源
    最近更新 更多