xamarin 在MAUI中导航后弹出菜单不可用

7vhp5slm  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(215)

我正在使用Xamarin Forms应用程序并将其转换为MAUI。
在Android中,我发现如果我导航到不同的页面,我会失去顶部导航栏中的弹出按钮(汉堡包按钮)。我还没有在iOS中尝试过,但我认为这是同样的行为。
以下是我导航到初始页面的方式:

private void LaunchInitialPage(Page page)
{
    FlyoutPage FlyoutPage = new FlyoutPage
    {
        Detail = new NavigationPage(page)
        {
            BarBackgroundColor = Helpers.Colors.BarBackgroundColor,
            BarTextColor = Color.FromArgb("#ffffff")
        },
        Flyout = new MyDrawerMenu(),
        FlyoutLayoutBehavior = FlyoutLayoutBehavior.Popover
    };
    ((App)Application.Current).MainPage = FlyoutPage;
}

上面的工作就像一个魅力。我的页面被正确地呈现,我看到了汉堡菜单。
大多数时候,我希望当用户点击某个项目时,“后退”按钮会显示在页面顶部。这是可行的。但是,有几个页面的后退按钮没有意义,所以我希望它被删除,而显示汉堡菜单。
以下是我的导航方式:

public async Task LaunchNextPage(Page page)
{
    Page mdp = (((App)Application.Current).MainPage as FlyoutPage).Detail;
    NavigationPage np = (NavigationPage)mdp;

    NavigationPage.SetHasBackButton(page, false);
    await np.PushAsync(page, true);
}

这段代码成功地导航到第二页。后退按钮按预期工作,并且NavigationPage.SetHasBackButton(page,false)从屏幕顶部的导航栏中删除了后退按钮。
然而,我没有看到汉堡菜单。我仍然可以从左侧滑动并显示菜单,但我真的希望看到汉堡显示在页面顶部。
我应该注意到,在Xamarin. Forms中,这确实如我预期的那样工作。只有在迁移到MAUI时,我才看到这种行为。
任何帮助将不胜感激!

w8rqjzmb

w8rqjzmb1#

我无法找到一个好的方法来恢复默认的汉堡包按钮,而不清除页面堆栈,我不想对所有页面都这样做。所以我能够通过在需要时设置标题视图和复制导航栏来使其工作。

public async Task LaunchNextPage(Page page)
{
    NavigationPage.SetHasBackButton(page, false);

    var fop = ((App)App.Current).MainPage as FlyoutPage;
    if (fop.Detail.Navigation.NavigationStack?.ToList()?.Count > 0)
    {
        NavigationPage.SetTitleView(this, new NavigationBar());
    }
    else
    {
        // at the beginning of the stack so already showing the burger menu
        NavigationPage.SetTitleView(this, null);
    }

    NavigationPage np = (NavigationPage)fop.Detail;
    await np.PushAsync(page, true);

    if (NavigationPage.GetTitleView(this) != null)
    {
        (NavigationPage.GetTitleView(this) as NavigationBar).Title = page.Title;
    }
}

NavigationBar.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<HorizontalStackLayout
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="NavigationBar" 
    VerticalOptions="CenterAndExpand" 
    HorizontalOptions="FillAndExpand" 
    Spacing="0"
    Padding="0"
    x:Name="this">

    <ImageButton
        x:Name="imgBurger"
        Source="hamburger_menu_icon.png" 
        WidthRequest="24" 
        HeightRequest="24"
        Margin="0,0,36,0"
        Clicked="imgBurger_Clicked"
        />

    <Label
        x:Name="lblNavBarTitle"
        VerticalOptions="Center"
        FontAttributes="Bold"
        FontSize="Medium" />

</HorizontalStackLayout>

NavigationBar.xaml.cs:

public partial class NavigationBar : HorizontalStackLayout
{ 
    public NavigationBar()
    {
        InitializeComponent();
        lblNavBarTitle.TextColor = Color.FromArgb("#FFFFFF");
        BackgroundColor = Color.FromArgb("#000000");
    }

    public string Title
    {
        set
        {
            lblNavBarTitle.Text = value;
        }
    }

    private void imgBurger_Clicked(object sender, EventArgs e)
    {
        var fop = ((App)App.Current).MainPage as FlyoutPage;
        fop.IsPresented = !fop.IsPresented;
    }
}

希望这能对某些人有所帮助!

相关问题