【问题标题】:How to create a xamarin forms cross platform hamburger slide view如何创建 xamarin 表单跨平台汉堡幻灯片视图
【发布时间】:2017-12-26 07:53:18
【问题描述】:

我正在编写一个 xamarin forms android 和 ios 跨平台应用程序。我想添加它,所以当您按下按钮时,它会创建一个汉堡包滑出式导航菜单,其中包含导航到另一个页面的按钮。我尝试观看教程,但由于我对 xamarin 还很陌生,所以没有一个教程能很好地解释我能够创建的内容。

我尝试按照本教程 here 进行操作,但我没有得到它,因为我已经有一个名为“MainPage”的主页和两个名为“SearchPage”和“ProfilePage”的页面,所以命名让我感到困惑,因为它们有其他页面和东西,然后迷路。

【问题讨论】:

  • 官方有一个很好很简单的指南。你检查了吗? developer.xamarin.com/guides/xamarin-forms/…
  • @EvZ 当我尝试创建 MainPageCS 页面时,“Master = MasterPage;”行表示名称 Master 在当前上下文中不存在
  • @Jacob 您应该创建自己的 MasterPage,并将您的详细信息页面作为 MasterPageItem 放入其中。

标签: c# xamarin xamarin.forms


【解决方案1】:

嗯,基本上,您有一个 MasterDetailPage,它既是“幻灯片视图”(在 Android 上称为 Drawer,在 Xamarin.Forms 上称为 Master),又是 ContentView(在 Xamarin.Forms 上称为 Detail) .这些视图中的每一个都是 Xamarin 上的一个页面。

您可以通过将这两个属性(Master 和 Detail)设置为 Page 来创建 MasterDetailPage,如下所示:

// MainPage here is the Propriety of the App class that controls the current displayed page
MainPage = new MasterDetailPage
{
    Master = new ContentPage { Content = new StackLayout { Children = { new Label { Text = "This is the Drawer Page!" } } } },
    Detail = new ContentPage { Content = new StackLayout { Children = { new Label { Text = "This is the Detail Page!" } } } }
};

将此添加到您的 App 类中,您将拥有一个显示 "This is the Drawer Page!" 的抽屉和一个显示 "This is the Detail Page!" 的详细信息页面。

现在,如果您将这些 ContentPages 分开在类中,将是这样的:

public class MasterPage : ContentPage
{
    public MasterPage()
    {
        Content = new StackLayout
        {
            Children =
            {
                new Label { Text = "This is the Master page!" }
            }
        }
    }
}

public class DetailPage : ContentPage
{
    public DetailPage()
    {
        Content = new StackLayout
        {
            Children =
            {
                new Label { Text = "This is the Detail page!" }
            }
        }
    }
}

// And in the App constructor
MainPage = new MasterDetailPage
{
    Master = new MasterPage(),
    Detail = new DetailPage()
};

就是这样......如果您对此有任何疑问,请随时提问:)


评论后编辑

此代码用于 App 类,其他类没有MainPage 属性。

如果您希望 MainPage 类是 MasterDetailPage,您可以从 MasterDetailPage (public class MainPage : MasterDetailPage) 扩展 MainPage,并且在登录后,而不是使用 Navigation 推送它,将 App 的 MainPage 设置为通过调用您的主页:

App.Current.MainPage = new MainPage 
{ 
    Master = MasterPage(), 
    Detail = DetailPage() 
};

【讨论】:

  • 当我的应用程序启动时,它导航到一个登录页面,然后导航到一个我称为“MainPage”的类,我尝试将顶部代码添加到我的 MainPage 构造函数,但它给出了错误。
  • 我仍然对如何实现这一点感到困惑,我只使用过 NavigationPages 并且是 Xamarin 的新手。我必须创建一个名为 MasterPageDetail 的类,其中包含什么?现在我在运行 App 构造函数时拥有它,它检查是否已经存在登录数据,如果有,则创建一个到 MainPage 的 NavigationPage 否则转到 LoginPage(在登录 LoginPage 后转到 MainPage) 我希望 MainPage 有带有选项的抽屉“主页”(又名 MainPage)和 SearchPage 的“搜索”
  • 试着理解我说的话。 MasterDetailPage 具有用于抽屉的属性和用于内容的属性。两个属性的类型都是 Page。只需将 MasterDetailPage 的 Master 属性设置为代表您的抽屉(幻灯片视图)的 Page 并将 Detail 属性设置为代表内容的页面(通常是其中包含 ContentPage 的 NavigationPage)
  • 我现在明白了,非常感谢您的帮助!完美运行!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
  • 2018-08-16
  • 2019-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多