我有一个针对 Android、IOS 和 UWP 的 Xamarin Forms 项目。在我的 Xamarin.Forms 项目 App.cs 中,我已将 MainPage 设置为 MasterDetailPage 继承类。我创建了一个自定义导航栏,所以我不想看到默认导航栏。
我已按照步骤从这个问题中隐藏了 IOS 和 Android 的默认栏。现在我也想为 UWP 隐藏它。
我在 Xamarin Forms 项目中尝试过:
NavigationPage.SetHasNavigationBar(this, false);
在 UWP 项目中(我同时尝试单独和全部):
Windows.UI.ViewManagement.StatusBar.GetForCurrentView().HideAsync();
Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().TryEnterFullScreenMode();
Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().SuppressSystemOverlays = true;
但没有任何帮助,所以在 UWP 中我仍然看到两个 menubuttons。在图像中,我喜欢隐藏灰色按钮(以及它所属的栏)。
如果我将 App.cs 中的 MainPage 设置为 NavigationPage 并且不使用 MasterDetailPage,则以下代码有助于删除栏:
NavigationPage.SetHasNavigationBar(this, false);
如何使用 MasterDetailPage 解决此问题?
谢谢你的帮助!
编辑:添加代码。
我在 MainPage 和 MenuPage 中看到导航栏。StackLayout 称为 Menubar 是我希望看到的唯一顶部栏。
应用程序.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace AddressReadApp2
{
public class App : Application
{
public App()
{
MainPage = new MasterDetail();
NavigationPage.SetHasNavigationBar(this, false);
NavigationPage.SetHasNavigationBar(MainPage, false);
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
}
MasterDetail.cs:
namespace AddressReadApp2
{
public class MasterDetail : MasterDetailPage
{
public MasterDetail ()
{
NavigationPage.SetHasNavigationBar(this, false);
Detail = new NavigationPage(new MainPage());
NavigationPage.SetHasNavigationBar(Detail, false);
BackgroundColor = Color.FromRgb(0, 170, 167);
Title = "ibi";
Master = new MenuPage();
MasterBehavior = MasterBehavior.Popover;
}
}
}
MenuPage.xaml.cs:
namespace AddressReadApp2.Pages
{
public partial class MenuPage : ContentPage
{
MasterDetail __parent = null;
MasterDetail _parent
{
get
{
if (__parent == null)
__parent = (MasterDetail)this.Parent;
return __parent;
}
}
MainPage __detail;
MainPage _detail
{
get
{
if (__detail == null && _parent != null)
__detail = (MainPage)((NavigationPage)_parent.Detail).CurrentPage;
return __detail;
}
}
public MenuPage()
{
NavigationPage.SetHasNavigationBar(this, false);
Title = "ibi";
//Icon = "ibi_logo.png";
InitializeComponent();
this.BackgroundColor = Color.FromRgb(0, 170, 167);
menu.GestureRecognizers.Add(new TapGestureRecognizer { Command = new Command(() => _parent.IsPresented = !_parent.IsPresented) });
Device.OnPlatform(
iOS: () =>
{
MenuBar.Padding = new Thickness(0, 25, 0, 0);
}
);
}
}
菜单页.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="AddressReadApp2.Pages.MenuPage">
<StackLayout Orientation="Vertical" VerticalOptions="FillAndExpand">
<StackLayout x:Name="MenuBar" Orientation="Horizontal">
<Image x:Name="menu" Source="menu.png" HeightRequest="35" WidthRequest="35" HorizontalOptions="Start" Margin="10,2,0,0" />
<Image x:Name="logo" Source="ibi_logo2.png" HeightRequest="35" WidthRequest="35" HorizontalOptions="EndAndExpand" Margin="0,0,10,2" />
</StackLayout>
<Grid RowSpacing="2" ColumnSpacing="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<!-- Between here are Labels for the menu -->
</Grid>
</StackLayout>
</ContentPage>
MainPage.xaml.xs:
namespace AddressReadApp2
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);
MenuBar.BackgroundColor = Color.White;
menu.GestureRecognizers.Add(new TapGestureRecognizer { Command = new Command(() => ((MasterDetailPage)Parent.Parent).IsPresented = true) });
}
}
}
MainPage.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="AddressReadApp2.MainPage" BackgroundColor="White" NavigationPage.TitleIcon="icon.png" x:Name="MainPage">
<StackLayout x:Name="mainStack" Orientation="Vertical" VerticalOptions="FillAndExpand">
<StackLayout x:Name="MenuBar" Orientation="Horizontal">
<Image x:Name="menu" Source="menu2.png" HeightRequest="35" WidthRequest="35" HorizontalOptions="Start" Margin="10,2,0,0" />
<Image x:Name="logo" Source="ibi_logo.png" HeightRequest="35" WidthRequest="35" HorizontalOptions="EndAndExpand" Margin="0,0,10,2" />
</StackLayout>
<StackLayout x:Name="pnlStart" Orientation="Vertical" VerticalOptions="Fill" HorizontalOptions="Fill">
<StackLayout x:Name="pnlWelcome" VerticalOptions="End" Padding="10">
<Label Text="Welcome" VerticalOptions="Center" HorizontalOptions="Center" FontSize="25" />
</StackLayout>
</StackLayout>
</StackLayout>
</ContentPage>