当我在 Mac 上的 Visual Studio 中创建新的 Xamarin.Forms 应用时,默认模板在 App
类中附带以下代码。
public partial class App : Application
{
public static bool UseMockDataStore = true;
public static string BackendUrl = "https://localhost:5000";
public App()
{
InitializeComponent();
if (UseMockDataStore)
DependencyService.Register<MockDataStore>();
else
DependencyService.Register<CloudDataStore>();
// What is the reason for this if statement?
if (Device.RuntimePlatform == Device.iOS)
MainPage = new MainPage();
else
MainPage = new NavigationPage(new MainPage());
}
}
上面代码中最后一个if语句的原因是什么?是因为 iOS 和 Android 之间的一些差异以及各自处理导航的方式吗?有人可以解释一下吗?
最佳答案
如果您查看 MainPage.cs,它是一个 TabbedPage
并且在将内容页面添加为选项卡时还有另一个 Device.RuntimePlatform
检查。如果是 iOS 页面,则添加为导航页面。而在 Android 页面的情况下,通常会添加。
public MainPage()
{
Page itemsPage, aboutPage = null;
switch (Device.RuntimePlatform)
{
case Device.iOS:
itemsPage = new NavigationPage(new ItemsPage())
{
Title = "Browse"
};
aboutPage = new NavigationPage(new AboutPage())
{
Title = "About"
};
itemsPage.Icon = "tab_feed.png";
aboutPage.Icon = "tab_about.png";
break;
default:
itemsPage = new ItemsPage()
{
Title = "Browse"
};
aboutPage = new AboutPage()
{
Title = "About"
};
break;
}
Children.Add(itemsPage);
Children.Add(aboutPage);
Title = Children[0].Title;
}
这导致以下导航堆栈结构:
iOS: MainPage -> NavigationPageRoot (ChildPages) -> OtherPages
Android NavigationPageRoot(MainPage) -> ChildPages -> OtherPages
现在这种结构上的差异是由于标签页的 native 实现不同造成的。 iOS 将标签页视为导航根,但在 Android 中,标签页只是导航根的子项。
即使页面结构不同,这也会在两个平台中描绘相似的导航行为。
注意:这可能仅适用于标签页。
希望这对您有所帮助。
关于android - 主页和导航设置 : iOS and Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47057650/