gpt4 book ai didi

c# - Xamarin Forms MasterDetailPage 主页隐藏菜单按钮

转载 作者:太空狗 更新时间:2023-10-29 19:45:02 28 4
gpt4 key购买 nike

我有一个针对 Android、IOS 和 UWP 的 Xamarin Forms 项目。在我的 Xamarin.Forms 项目 App.cs 中,我已将 MainPage 设置为 MasterDetailPage 继承类。我已经创建了一个自定义导航栏,所以我不想看到默认设置。

我已经按照步骤隐藏了 IOS 和 Android 的默认栏 from this question .现在我也想为 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 中我仍然看到 two menubuttons .在图像中,我想隐藏灰色按钮(及其所属的栏)。

如果我将 App.cs 中的 MainPage 设置为 NavigationPage 并且不使用 MasterDetailPage,则以下代码有助于删除栏:

NavigationPage.SetHasNavigationBar(this, false);

如何使用 MasterDetailPage 解决这个问题?

感谢您的帮助!

编辑:添加代码。

我在 MainPage 和 MenuPage 中看到了导航栏。名为 Menubar 的 StackLayout 是我希望看到的唯一顶部栏。

应用程序:

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

主页.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>

最佳答案

我觉得可以

public class CustomMasterDetailRenderer :MasterDetailPageRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<MasterDetailPage> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.CollapsedPaneWidth = 0;
Control.CollapseStyle = Xamarin.Forms.PlatformConfiguration.WindowsSpecific.CollapseStyle.Partial;
Control.MasterToolbarVisibility = Windows.UI.Xaml.Visibility.Collapsed;
Control.DetailTitleVisibility = Windows.UI.Xaml.Visibility.Collapsed;
Control.MasterTitleVisibility = Windows.UI.Xaml.Visibility.Collapsed;
Control.ContentTogglePaneButtonVisibility =
Windows.UI.Xaml.Visibility.Collapsed;
}
}
}

现在你可以这样调用我认为你的调用和代码是正确的,但你需要为 UWP 自定义渲染

NavigationPage.SetHasBackButton(this, false);
NavigationPage.SetHasNavigationBar(this, false);

关于c# - Xamarin Forms MasterDetailPage 主页隐藏菜单按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41224320/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com