代码之家  ›  专栏  ›  技术社区  ›  David B

在xamrin窗体上复制工具栏

  •  0
  • David B  · 技术社区  · 6 年前

    我在使用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

    <?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());
     }
    

    您可以清楚地看到,在内容工具栏下面的图像中复制的问题。 enter image description here

    1 回复  |  直到 6 年前
        1
  •  2
  •   DavidS    6 年前

    您正在导航堆栈中嵌套导航页,这不是您想要做的。getMainPage()返回子级为MainPage的NavigationPage。mainpage本身是tabbedpage,它的每个子页面都是一个navigationpage。NavigationPage的每个可见层都将显示一个工具栏,这就是您看到这个的原因。

    tabbedpage的最佳实践是将其用作导航堆栈的根,并将navigationpage实例(或contentpage实例)作为其子级。因此,只要清除导航堆栈,额外的工具栏就会消失:

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