簡體   English   中英

如何從橫向菜單上的彈出項進行推送

[英]How to make a push from flyout items on lateral menu

我使用 Shell 創建了一個 Xamarin.Forms 應用程序來管理 TabBar 和 Hamburgger 菜單,我需要將菜單項作為詳細信息頁面推送以進行正確導航

我創建下一個代碼:

<Shell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Pages.BasePage"
FlyoutBehavior="Flyout"
Shell.TitleColor ="Black"
Shell.TabBarBackgroundColor="#F4F4F4"
Shell.TabBarUnselectedColor="Gray"
Shell.NavBarHasShadow="True"
Shell.PresentationMode="Animated"
x:Name="shell">
<TabBar x:Name="bottomBar" Route="main">
    <Tab Title="Tab 1" Icon="img1.png">
        <ShellContent Route="tab-1-page" ContentTemplate="{DataTemplate views:page1}" />
    </Tab>
    <Tab Title="Tab 2" Icon="img2.png">
        <ShellContent Route="tab-2-page" ContentTemplate="{DataTemplate views:page2}" />
    </Tab>
</TabBar>

<MenuItem Text="hamburg menu item 1"
    IconImageSource="menu1.png"
    Command="{Binding NavigateCommand}"
    CommandParameter="menu1-page" />
<MenuItem Text="hamburg menu item 2"
    IconImageSource="menu2.png"
    Command="{Binding NavigateCommand}"
    CommandParameter="menu2-page" /> 
<MenuItem Text="hamburg menu item 3"
    IconImageSource="menu3.png"
    Command="{Binding NavigateCommand}"
    CommandParameter="menu3-page" />  

<Shell.FlyoutHeader>
    <StackLayout BackgroundColor="white">
        <Image Source="title.png" VerticalOptions="FillAndExpand" HorizontalOptions="CenterAndExpand" WidthRequest="180" />
    </StackLayout>
</Shell.FlyoutHeader>

代碼隱藏是:

public BasePage()
{
    InitializeComponent();
    BindingContext = new BaseViewModel();  
}

而 ViewModel 是:

using HandHeldCashier.Pages;
using System;
using System.Collections.Generic;
using System.Windows.Input;
using Xamarin.Forms;

public class BaseViewModel
{
    public Dictionary<string, Type> Routes { get; private set; } = new Dictionary<string, Type>();
    public ICommand NavigateCommand => new Command<string>((route) => Shell.Current.GoToAsync(route));
    public BaseViewModel() { }

    public void RegisterRoutes()
    {
        Routes.Add("detail1-page", typeof(Detail1Page));
        Routes.Add("detail2-page", typeof(Detail2Page));
        Routes.Add("detail3-page", typeof(Detail3Page));    
        Routes.Add("menu1-page", typeof(AboutPage));
        Routes.Add("menu2-page", typeof(CommentsPage));
        Routes.Add("menu3-page", typeof(SettingsPage));            

        foreach (var item in Routes)
        {
            Routing.RegisterRoute(item.Key, item.Value);
        }
    }
}

第一次用這個不行,回去再訪問就正常了

有誰知道如何替換實現或糾正這個錯誤?

我制作了一個示例代碼來檢查。 它對我有用。 請在下面檢查。

  1. 確保您已注冊路線。 BaseViewModel結構中運行RegisterRoutes方法。

     public BaseViewModel() { RegisterRoutes(); } public void RegisterRoutes() { Routes.Add("detail1-page", typeof(Detail1Page)); Routes.Add("detail2-page", typeof(Detail2Page)); Routes.Add("detail3-page", typeof(Detail3Page)); Routes.Add("menu1-page", typeof(AboutPage)); Routes.Add("menu2-page", typeof(CommentsPage)); Routes.Add("menu3-page", typeof(SettingsPage)); foreach (var item in Routes) { Routing.RegisterRoute(item.Key, item.Value); } }
  2. 為了獲得更好的視覺效果,您可以在導航菜單項時關閉彈出按鈕。

     public ICommand NavigateCommand => new Command<string>((route) => { Shell.Current.GoToAsync(route); Shell.Current.FlyoutIsPresented = false; });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM