【问题标题】:Xamarin Forms - Duplicate Toolbar Appearing On Pages IssueXamarin Forms - 重复的工具栏出现在页面问题上
【发布时间】:2020-10-21 10:45:53
【问题描述】:

我正在构建一个购物车,但我遇到了一个非常烦人的问题,即我的页面上显示了双工具栏。

我花了几个小时解决这个问题。

我当前的页面设置如下:

  1. MainHomePage(这包含一个菜单和一个标题为“MainHomePageDetail”的嵌入页面)

  1. MainHomePageDetail(其中包含用户单击它的图像列表,它也会将它们带到其他页面。

  1. PageBulkBuys(这是显示产品详细信息等的页面之一)

问题待解决。

  • 我只想删除当前显示在主页和子页面上的双工具栏。

请注意,如果我单击其中一个菜单链接,此问题就会消失,并且只显示一个工具栏。

但是如果我点击主页中的一个链接,它会显示一个双工具栏,这真的很奇怪。

我在网上尝试了一些解决方案,但没有成功。

这是我的代码:

App.CS

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

MainHomePage XAML

<?xml version="1.0" encoding="utf-8"?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="xxxxx.MainHomePage" xmlns:pages="clr-namespace:xxxxx">
    <MasterDetailPage.Master>
        <pages:MainHomePageMaster x:Name="MasterPage" />
    </MasterDetailPage.Master>
    <MasterDetailPage.Detail>
        <NavigationPage BarBackgroundColor="Black">
            <x:Arguments>
                <pages:MainHomePageDetail />
            </x:Arguments>
        </NavigationPage>
    </MasterDetailPage.Detail>
</MasterDetailPage>

MainHomePage CS

public partial class MainHomePage : MasterDetailPage
    {
        public MainHomePage()
        {
            InitializeComponent();
            MasterPage.ListView.ItemSelected += ListView_ItemSelected;
        }
        
        [Obsolete]
        private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            var item = e.SelectedItem as MainHomePageMenuItem;
            if (item == null)
                return;
            if (item.Id == 10) // BulkBuys
                Navigation.PushAsync(new BulkBuys());
        }
    }

MainHomePageDetail 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="xxxx.MainHomePageDetail" Title="xxxx" BackgroundColor="Black">
   <ContentPage.ToolbarItems>  
        <ToolbarItem Name="shoppingcarticon" IconImageSource="xxxxxx.png" Priority="0" Order="Primary" Activated="ShoppingCartClicked"/>  
   </ContentPage.ToolbarItems> 
    <StackLayout Padding="10">
        <ScrollView HorizontalOptions="FillAndExpand">
        <StackLayout>

            <Image Source="xxxxx.png" WidthRequest="600" HeightRequest="50"/>

            <Label x:Name="labelLoggedInUser" TextColor="White" FontAttributes="Bold" FontSize="18"></Label>

            <!-- Banner 1 -->
            <Frame x:Name="frame1" BackgroundColor="#2e2e2e">
                <StackLayout>
                    <Image x:Name="Banner1Image" WidthRequest="600" HeightRequest="200">
                        <Image.GestureRecognizers>
                        <TapGestureRecognizer
                            Tapped="BtnBulkBargains"
                            NumberOfTapsRequired="1" />
                        </Image.GestureRecognizers>
                    </Image>
                    <Frame BackgroundColor="Green" HasShadow="False" Padding="5" HorizontalOptions="Center" WidthRequest="250" HeightRequest="20" CornerRadius="00">
                        <Label x:Name="Banner1Text" FontAttributes="Bold" FontSize="18" TextColor="White" WidthRequest="80" HorizontalTextAlignment="Center" ></Label>
                    </Frame>
                    <Label x:Name="Banner1Header" TextColor="White" HorizontalTextAlignment="Center"></Label>
                 </StackLayout>
            </Frame>

            <!-- Banner 2 -->
            <Frame x:Name="frame2" BackgroundColor="#2e2e2e">
                <StackLayout>
                    <Image x:Name="Banner2Image" WidthRequest="600" HeightRequest="125"></Image>
                </StackLayout>
            </Frame>

MainHomePageDetail CS

public MainHomePageDetail()
   {
       InitializeComponent();
   }
private void ShoppingCartClicked(object sender, EventArgs e)
   {
       Navigation.PushAsync(new ViewFBShoppingCart());
   }

BulkBuysPage 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="xxxx.PageBulkBuys"
             Title="Bulk Buys"
             BackgroundColor="Black"
             NavigationPage.HasBackButton="True">
    <ContentPage.ToolbarItems>  
        <ToolbarItem Name="shoppingcarticon" IconImageSource="xxxx.png" Priority="0" Order="Primary" Activated="ShoppingCartClicked"/>  
   </ContentPage.ToolbarItems> 
    <ContentPage.Content>
        <StackLayout>

            <Image Source="xxxx.png" WidthRequest="600" HeightRequest="50"/>

            <ListView x:Name="productsListView"
                      HasUnevenRows="True"                       
                        VerticalOptions="FillAndExpand"
                      SeparatorVisibility="None"
                      ItemSelected="OnItemSelected">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <ViewCell.View>
                                <Frame HasShadow="True" Padding="20" Margin="20">
                                    <StackLayout>
                                        <Image Source="{Binding featured_src}"/>
                                        <Label x:Name="labelProductTitle" Text="{Binding title}" FontSize="Medium" />
                                        <Frame BackgroundColor="Red" Padding="5" HorizontalOptions="Center" WidthRequest="80" HeightRequest="20" CornerRadius="00">
                                            <Label WidthRequest="40" Text="{Binding price, StringFormat='${0}'}" TextColor="White" HorizontalTextAlignment="Center"></Label>
                                        </Frame>
                                    </StackLayout>
                                </Frame>
                            </ViewCell.View>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
            
        </StackLayout>
    </ContentPage.Content>

</ContentPage>

BulkBuysPage CS

public PageBulkBuys()
   {
        InitializeComponent();
   }
private void ShoppingCartClicked(object sender, EventArgs e)
   {
      Navigation.PushAsync(new ViewFBShoppingCart());
   }

我们将不胜感激。

【问题讨论】:

    标签: c# ios xamarin.forms


    【解决方案1】:

    因为你在App.cs中使用了NavigationPage,它会为你创建一个工具栏:

    MainPage = new NavigationPage(new MainHomePage());
    

    尝试将其更改为:

    MainPage = new MainHomePage();
    

    或在MainHomePage 中使用NavigationPage.SetHasNavigationBar 方法隐藏工具栏:

    public App()
        {
            InitializeComponent();
            MainPage = new NavigationPage(new MainHomePage());
        }
    
    public MainHomePage()
        {
            InitializeComponent();
            NavigationPage.SetHasNavigationBar(this, false);
            MasterPage.ListView.ItemSelected += ListView_ItemSelected;
        }
    

    【讨论】:

    • 谢谢@LeoZhu-MSFT 我已经尝试过了,它确实删除了双工具栏,但是当我尝试单击它们并显示错误时,它会打开另一个问题,即菜单不再起作用并显示错误“ iOS 上不支持 PushAsync,请使用 NavigationPage。”
    • @Khoa 我明白了,你在MainHomePage 中调用Navigation.PushAsync,这意味着你要打开的新页面与你的MainHomePage 在同一个堆栈中,你可以尝试另一种方法我在上面更新以隐藏导航栏。
    • 有效!谢谢@LeoZhu-MSFT! :D 我希望你在 2021 年及以后变得富有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 2021-07-16
    • 2014-09-13
    相关资源
    最近更新 更多