android - 主页和导航设置 : iOS and Android

标签 android ios xamarin.forms

当我在 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/

相关文章:

android - NEXT_ALARM_FORMATTED 已弃用

android - 在 Android Q/API 29 上列出 WiFi 网络

c# - Xamarin.Forms:使用披露按钮时删除缩进

c# - Android构建错误 "failed to create JavaTypeInfo for class":Xamarin

c# - Xamarin Forms UWP - 无法调试共享代码

Android 2.2 以编程方式判断我的蓝牙耳机是否在应用程序启动时连接

android - 在具有大量文本行和样式的 EditText 中启用 2D 滚动

iphone - iOS 漫画阅读器 - 这是一个可行的设计吗?

ios - 如何在 Objective-C 中为 pin mapview 中的文本添加事件处理程序?

c# - 在 MonoTouch 中获取没有 UI 的 iOS 屏幕分辨率