嗯,基本上,您有一个 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()
};