【问题标题】:Couldn't get basic Hamburger Master Detail view无法获得基本的 Hamburger Master Detail 视图
【发布时间】:2016-12-25 15:31:57
【问题描述】:

查看所有问题和论坛后,我仍然无法获得带有汉堡图标的主详细信息页面。如果我将此主详细信息页面用作应用程序启动,那么我会看到该功能按预期工作。请看下面的代码

DashBoadCreator.xaml `

<MasterDetailPage.Master>

</MasterDetailPage.Master>

<MasterDetailPage.Detail>

  </x:Arguments>
</NavigationPage>
</MasterDetailPage.Detail>
`

`public partial class DashBoadCreator : MasterDetailPage
{
DashboardMaster master;
DashboardDetail1 detil;
public DashBoadCreator()
{
InitializeComponent();
master = new DashboardMaster();
detil = new DashboardDetail1();

        Master = master;
        Master.Title = "this is a title";
        Master.Icon = "icon.png";
        Detail = new NavigationPage(detil);

    }

    // Event for Menu Item selection, here we are going to handle navigation based
    // on user selection in menu ListView

}`
DashboadMaster.xaml
`

<!-- 
         This StackLayout you can use for other
         data that you want to have in your menu drawer

    <StackLayout BackgroundColor="#e74c3c"
                 HeightRequest="75">

      <Label Text="Some Text title"
             FontSize="20"
             VerticalOptions="CenterAndExpand"
             TextColor="White"
             HorizontalOptions="Center"/>
    </StackLayout>         -->

<ListView x:Name="navigationDrawerList"
          RowHeight="60"
          SeparatorVisibility="None"
          BackgroundColor="#e8e8e8"
          ItemSelected="OnMenuItemSelected">

  <ListView.ItemTemplate>
    <DataTemplate>
      <ViewCell>

        <!-- Main design for our menu items -->
        <StackLayout VerticalOptions="FillAndExpand"
                     Orientation="Horizontal"
                     Padding="20,10,0,10"
                     Spacing="20">

          <Image Source="{Binding Icon}"
                 WidthRequest="40"
                 HeightRequest="40"
                 VerticalOptions="Center" />

          <Label Text="{Binding Title}"
                 FontSize="Medium"
                 VerticalOptions="Center"
                 TextColor="Black"/>
        </StackLayout>
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>
`

` public partial class DashboardMaster : ContentPage {

public List<MasterPageItem> menuList { get; set; }

public DashboardMaster()
{
    Title = "samples";
    InitializeComponent();

    menuList = new List<MasterPageItem>();

    // Creating our pages for menu navigation
    // Here you can define title for item, 
    // icon on the left side, and page that you want to open after selection
    var page1 = new MasterPageItem() { Title = "Item 1", Icon = "itemIcon1.png", TargetType = typeof(DashboardDetail1) };
    var page2 = new MasterPageItem() { Title = "Item 2", Icon = "itemIcon2.png", TargetType = typeof(DashboardDetail1) };
    var page3 = new MasterPageItem() { Title = "Item 3", Icon = "itemIcon3.png", TargetType = typeof(DashboardDetail1) };
    var page4 = new MasterPageItem() { Title = "Item 4", Icon = "itemIcon4.png", TargetType = typeof(DashboardDetail1) };
    var page5 = new MasterPageItem() { Title = "Item 5", Icon = "itemIcon5.png", TargetType = typeof(DashboardDetail1) };
    var page6 = new MasterPageItem() { Title = "Item 6", Icon = "itemIcon6.png", TargetType = typeof(DashboardDetail1) };
    var page7 = new MasterPageItem() { Title = "Item 7", Icon = "itemIcon7.png", TargetType = typeof(DashboardDetail1) };
    var page8 = new MasterPageItem() { Title = "Item 8", Icon = "itemIcon8.png", TargetType = typeof(DashboardDetail1) };
    var page9 = new MasterPageItem() { Title = "Item 9", Icon = "itemIcon9.png", TargetType = typeof(DashboardDetail1) };

    // Adding menu items to menuList
    menuList.Add(page1);
    menuList.Add(page2);
    menuList.Add(page3);
    menuList.Add(page4);
    menuList.Add(page5);
    menuList.Add(page6);
    menuList.Add(page7);
    menuList.Add(page8);
    menuList.Add(page9);

    // Setting our list to be ItemSource for ListView in MainPage.xaml
    navigationDrawerList.ItemsSource = menuList;
    //Application.Current.MainPage = new DashboardMaster();
    // NavigationPage.SetHasNavigationBar(this, false);

}
private void OnMenuItemSelected(object sender, SelectedItemChangedEventArgs e)
{
    DashBoadCreator creator = new DashBoadCreator();

    var item = (MasterPageItem)e.SelectedItem;
    Type page = item.TargetType;

    creator.Detail = new NavigationPage((Page)Activator.CreateInstance(page));
    creator.IsPresented = false;
}

}

<ListView.ItemTemplate>


          <Image Source="{Binding Icon}"
                    WidthRequest="40"
                    HeightRequest="40"
                    VerticalOptions="Center" />

          <Label Text="{Binding Title}"
                 FontSize="Medium"
                 VerticalOptions="Center"
                 TextColor="Black"/>
         <!-- <Label Text="{Binding Description}"
                 FontSize="Small"
                 VerticalOptions="End"/>-->
          <StackLayout VerticalOptions="End" HorizontalOptions="End">
            <Button Text="Apply" FontSize="10"  TextColor="Green" BorderWidth="1" HorizontalOptions="End" />
          </StackLayout>
        </StackLayout>

      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>

</ListView>

【问题讨论】:

    标签: xamarin xamarin.forms portable-class-library


    【解决方案1】:

    为母版页设置图标以查看汉堡图标

    示例代码:

    public masterpage()
    {
         Icon = "hamburger.png";
    }
    

    【讨论】:

    • 当您将 MasterDetailPage 作为 App.Xaml.cs 中的根页面时,一切都按预期工作。在我的情况下,我需要用户根据操作登录/注册,然后使用以下代码导航到 MasterDetail 页面 Application.Current.MainPage = new NavigationPage(new MenuPage()); Navigation.PopToRootAsync(true);这次汉堡菜单消失了。现在补充一下,如果你物理地改变你的设备方向(在模拟器上改变它并在自动旋转的 Android 手机上尝试它),它会神奇地出现!!!!!!!!!我该如何解决这个问题?
    • 我必须在母版页中的内容布局上设置图标。我还为我的详细信息页面使用导航页面。
    【解决方案2】:

    我按照一些不同的教程编写了一个工作演示,并获得了其中最重要的部分。所以,至少对我来说,这是一个更简单的演示,我可以得到它。

    查看此链接。

    My github working demo

    Intervection 是一个随机的详细页面(按参数构造)。

    如果您有任何疑问,请告诉我。

    干杯,伙计。圣诞快乐。

    【讨论】:

    • 我正在开发 Xamain 表单应用程序,所以您的代码可能对我没有帮助。同时我有一个我认为的解决方案。我会尝试一下并发布答案。
    • 我会尽力让你知道的。我没有看到代码。命名约定让我有点失望。
    • 别担心。试试看,让我知道任何疑问。我在自己的跨应用程序中测试了它的主从菜单。带有多个详细页面的母版页链接,所有这些页面都为单个页面定义,并由我称之为该单个页面的构造函数的参数区分。如果您认为我的努力值得,请评价我的回答。干杯伙伴。
    【解决方案3】:

    在一些发现之后,我来到了这个讨论

    Missing menu button on MasterDetailPage when assigning App.MainPage

    使用这些发现,我将 app.xaml.cs 更改如下

     MainPage = new NavigationPage(new MenuPage());
     MasterDetailTest.App.Current.MainPage.Navigation.PushAsync(new MainPage());
    

    现在母版页始终是根页,汉堡菜单按预期工作。对于其他遇到此问题的人,希望这对您有所帮助

    【讨论】:

      猜你喜欢
      • 2021-11-11
      • 1970-01-01
      • 2016-03-25
      • 1970-01-01
      • 2012-11-23
      • 1970-01-01
      • 2013-09-03
      • 2020-05-30
      • 1970-01-01
      相关资源
      最近更新 更多