Xamarin Forms iOS导航PushAsync在NavigationBar上显示母版页标题

时间:2019-02-20 15:02:09

标签: xamarin xamarin.forms xamarin.ios

在我的xamarin.forms项目中,第1页是一个详细页面,在navigatonbar上具有母版页标题。当我使用PushAsync从page1视图模型导航到page3时,page3同时具有母版页标题和导航栏后退按钮。当我从母版页面内部从page1视图模型导航到page2时,page2仅具有navbackbutton,没有主标题。从page2视图模型导航到page3,page3也没有母版标题,只有navbackbutton。我在Android上没有此问题。在iOS上从page1 viewmodel导航到page3时如何隐藏母版页标题?

page1 cs:调用移动到page3的vm方法

private async void Cart_Tapped(object sender, EventArgs e)
        {
            await img_cart.FadeTo(0.3, 500);
            await img_cart.FadeTo(1, 500);
            var vm = BindingContext as Main_page_vm;
            vm?.Save_cart();             
        }

从第1页视图模型导航到第3页:详细页面到有问题的内容页面

internal async void Save_cart()
        {
            if(Number.Equals(0))
            {
                 App.Current.Properties["cartitems"] = purchaselist;
                DependencyService.Get<IToast>().LongAlert("Empty cart !");
            }
            else
            {
                App.Current.Properties["cartitems"] = purchaselist;

                List<tb_login> Login_list = App.Database.Get_user_id();
                if (!Login_list.Count.Equals(0))
                {
                    foreach (tb_login tb_Login in Login_list)
                    {
                        App.Current.Properties["user_id"] = tb_Login.user_id;
                        App.Current.Properties["username"] = tb_Login.user_name;
                    }
                    await Navigation.PushAsync(new Address_page2()); //to page3,now showing master 
                }
                else
                {
                    await Navigation.PushAsync(new LoginPage());
                }
            }
        }

第1页到第2页:详细页面到内容页面,没有问题

        private async void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            var item = e.SelectedItem as HomePageMenuItem;
            if (item == null)
                return;
            if (item.Id == 0)
            {
               // Detail = new NavigationPage(new HomelyPage());
                await Navigation.PushAsync(new HomelyPage());//to page2 has no master issues
            }
            IsPresented = false;

            MasterPage.ListView.SelectedItem = null;
        }

第2页到第3页的视图模型:从内容页面到内容页面,没有问题

public async void Save_cart()
        {
            if (Number.Equals(0))
            {
                 App.Current.Properties["cartitems"] = purchaselist;
                DependencyService.Get<IToast>().LongAlert("Empty cart !");
            }
            else
            {
                App.Current.Properties["cartitems"] = purchaselist;

                List<tb_login> Login_list = App.Database.Get_user_id();
                if (!Login_list.Count.Equals(0))
                {
                    foreach (tb_login tb_Login in Login_list)
                    {
                        App.Current.Properties["user_id"] = tb_Login.user_id;
                        App.Current.Properties["username"] = tb_Login.user_name;
                    }
                    await Navigation.PushAsync(new Address_page2()); //to page3,also not showing master
                }
                else
                {
                    await Navigation.PushAsync(new LoginPage());
                }
            }
        }

page1 xaml:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="OnRestaur.Views.Main_Page"
             BackgroundColor="#f2f2f2"
             NavigationPage.HasBackButton="False">
    <NavigationPage.TitleView>
        <StackLayout HorizontalOptions="EndAndExpand">
            <Grid HorizontalOptions="EndAndExpand">
                <Label Text="{Binding Number, Mode=TwoWay}" FontAttributes="Bold" HorizontalOptions="EndAndExpand" FontSize="Small" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" Margin="0,0,32,10"  Style="{StaticResource Label_font}" TextColor="White">
                </Label>
                <Image Source="shoppingcart_white.png" x:Name="img_cart" HorizontalOptions="EndAndExpand" WidthRequest="60" HeightRequest="50" Margin="0,0,10,0">
                    <Image.GestureRecognizers>
                        <TapGestureRecognizer Tapped="Cart_Tapped" />
                    </Image.GestureRecognizers>
                </Image>
            </Grid>
        </StackLayout>        
    </NavigationPage.TitleView>

Page3 xaml:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="OnRestaur.Views.Address_page2"
             NavigationPage.HasBackButton="False"
             Title=" ">   
    <NavigationPage.TitleView>
        <StackLayout HorizontalOptions="FillAndExpand" Orientation="Horizontal" Margin="0" Spacing="0" Padding="0">            
            <Image Source="back_arrow.png" x:Name="img_cart" HorizontalOptions="StartAndExpand" WidthRequest="30" HeightRequest="30" Margin="0,0,0,0">
                <Image.GestureRecognizers>
                    <TapGestureRecognizer Tapped="Back_arrow_Tapped" />
                </Image.GestureRecognizers>
            </Image>
        </StackLayout>         
    </NavigationPage.TitleView>

page2 xaml:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             Title="Home"
             x:Class="OnRestaur.Views.HomelyPage"
             xmlns:local="OnRestaur.Views;assembly=ContentView"
             xmlns:custom="clr-namespace:ImageCircle.Forms.Plugin.Abstractions;assembly=ImageCircle.Forms.Plugin"
             NavigationPage.HasBackButton="False">
    <NavigationPage.TitleView>
        <StackLayout HorizontalOptions="FillAndExpand" Orientation="Horizontal">
            <Image Source="back_arrow.png"   HorizontalOptions="StartAndExpand" WidthRequest="30" HeightRequest="30" Margin="0,0,0,0">
                <Image.GestureRecognizers>
                    <TapGestureRecognizer Tapped="Back_arrow_Tapped" />
                </Image.GestureRecognizers>
            </Image>
            <Grid HorizontalOptions="EndAndExpand">                
                <Image Source="loc.png" x:Name="img_cart" HorizontalOptions="EndAndExpand" WidthRequest="20" HeightRequest="20" Margin="0,0,30,0">
                    <Image.GestureRecognizers>
                        <TapGestureRecognizer Tapped="Loc_Tapped" />
                    </Image.GestureRecognizers>
                </Image>
            </Grid>
        </StackLayout>
    </NavigationPage.TitleView>

1 个答案:

答案 0 :(得分:1)

将导航更改为如下所示,它应该可以解决您的问题:

Application.Current.MainPage.Navigation.PushAsync(new Address_page2());