2017-11-01 8 views
0

MacでVisual Studioで新しいXamarin.Formsアプリケーションを作成すると、デフォルトのテンプレートにはAppクラスに次のコードが含まれています。メインページとナビゲーションの設定:iOSとAndroid

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の違いと、それぞれがどのようにナビゲーションを処理するのか?誰か説明してもらえますか?

答えて

1

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:メインページ - > NavigationPageRoot(ChildPages) - > OtherPages

アンドロイド NavigationPageRoot(メインページ) - > ChildPages - >その他のページ

この構造の違いは、タブ付きページのネイティブ実装の違いによるものです。 iOSではタブページはナビゲーションルートと見なされますが、Androidタブページではナビゲーションルートの子にすぎません。

これは、ページ構造が異なっていても、両方のプラットフォームで同様のナビゲーション動作を示します。

注:これは、タブ付きページの場合のみ可能です。

これが役に立ちます。

関連する問題