什么是iOS的UITabBarController的Android等价物?

时间:2018-04-01 03:32:47

标签: xamarin xamarin.forms xamarin.android

我在iOS渲染器中有以下代码:

public class TabbedPageRenderer : TabbedRenderer
{
   protected override void OnElementChanged(VisualElementChangedEventArgs e)
   {
      base.OnElementChanged(e);
      try
      {
         var tabbarController = (UITabBarController)this.ViewController;

         if (null != tabbarController)
         {
            tabbarController.ViewControllerSelected += OnTabBarReselected;
         }
       }
       catch (Exception exception)
       {
          Console.WriteLine(exception);
       }
    }

    void OnTabBarReselected(object sender, UITabBarSelectionEventArgs e)
    {
        var tabs = Element as TabbedPage;
        var playTab = tabs.Children[4];

        if (TabBar.SelectedItem.Title == "Play")
        {
            if (tabs != null)
            {
                playTab.Title = "Pause";
                playTab.Icon = "ionicons_2_0_1_pause_outline_22.png";
            }
            App.pauseCard = false;
        }
        else 
        {
            if (tabs != null)
            {
               playTab.Title = "Play";
               playTab.Icon = "ionicons_2_0_1_play_outline_25.png";
            }
               App.pauseCard = true;
        }
    }
}

这样做是让用户暂停/播放在页面中运行的计时器。打开应用时,Home标签将会打开,以显示Play图标。但是当切换到Play选项卡时,默认情况下计时器正在运行,因此会显示Pause标题和图标。

上面的代码适用于iOS。但我仍然迷失在Android中。我为Android尝试了以下代码:

public class MyTabbedPageRenderer: TabbedPageRenderer, TabLayout.IOnTabSelectedListener
{
   void TabLayout.IOnTabSelectedListener.OnTabReselected(TabLayout.Tab tab)
   {
      var tabs = Element as TabbedPage;
      var playTab = tabs.Children[4];
      var selectedPosition = tab.Position;

      if(selectedPosition == 4) 
      {
         if (playTab.Title == "Play")
         {
            if (tabs != null)
            {
               playTab.Title = "Pause";
               playTab.Icon = "ionicons_2_0_1_pause_outline_22.png";
            }
            App.pauseCard = false;
          }
          else
          {
             if (tabs != null)
             {
                playTab.Title = "Play";
                playTab.Icon = "ionicons_2_0_1_play_outline_25.png";
             }
             App.pauseCard = true;
           }
         }
    }
}

显然这只会在重新选择标签时起作用。如果有人能指出我正确的方向,我真的很感激。

1 个答案:

答案 0 :(得分:1)

与@ G.hakim一样,您还需要添加TabLayout.IOnTabSelectedListener.OnTabSelected方法,它与OnTabReselected相同:

   void TabLayout.IOnTabSelectedListener.OnTabSelected(TabLayout.Tab tab)
   {
      var tabs = Element as TabbedPage;
      var playTab = tabs.Children[4];
      var selectedPosition = tab.Position;

      if(selectedPosition == 4) 
      {
         if (playTab.Title == "Play")
         {
            if (tabs != null)
            {
               playTab.Title = "Pause";
               playTab.Icon = "ionicons_2_0_1_pause_outline_22.png";
            }
            App.pauseCard = false;
          }
          else
          {
             if (tabs != null)
             {
                playTab.Title = "Play";
                playTab.Icon = "ionicons_2_0_1_play_outline_25.png";
             }
             App.pauseCard = true;
           }
         }
    }