2017-07-18 63 views
0

我想在導航欄的左側設置一個ToolbarItem,但我不知道如何設置它。 Xamarin.Forms的默認ToolbarItem在右側如何在Xamarin.Forms左側設置ToolbarItem?

+0

在特定平臺上?你會需要一個自定義的渲染器,因爲像你說的那樣;默認情況下不支持 –

回答

0

在iOS中我使用自定義渲染器,我不認爲它有任何方法。在Android中,我並不打擾,因爲人們習慣於右側的項目。下面的渲染器檢查是否有多個ToolBarItems,如果是,則將其移動到左側。

[assembly: ExportRenderer(typeof(ContentPage), typeof(LeftNavigationItemRenderer))] 
namespace MyApp.iOS 
{ 
    public class LeftNavigationItemRenderer : PageRenderer 
    { 
     public override void ViewWillAppear(bool animated) 
     { 
      base.ViewWillAppear(animated); 

      if (NavigationController == null) 
       return; 

      var navigationItem = NavigationController.TopViewController.NavigationItem; 
      var leftNativeButtons = (navigationItem.LeftBarButtonItems ?? new UIBarButtonItem[] { }).ToList(); 
      var rightNativeButtons = (navigationItem.RightBarButtonItems ?? new UIBarButtonItem[] { }).ToList(); 

      if (rightNativeButtons.Count > 1) 
      { 
       var nativeItem = rightNativeButtons.Last(); 
       rightNativeButtons.Remove(nativeItem); 
       leftNativeButtons.Add(nativeItem); 
      } 

      navigationItem.RightBarButtonItems = rightNativeButtons.ToArray(); 
      navigationItem.LeftBarButtonItems = leftNativeButtons.ToArray(); 
     } 
    } 
} 
+0

謝謝,但我想在Xamarin.Forms中實現它,而不是在特定的平臺,iOS或Android上實現。 – Jackson

+0

我很抱歉,但這不是Xamarin Forms的工作方式。沒有訴諸特定於平臺的代碼,有些事情根本無法實現,這就是其中之一。開箱即用的Xamarin Forms的功能有限,但它在擴展性方面非常強大。這確實意味着你大多將不得不編寫特定於平臺的代碼。 –

+0

好的,謝謝,我明白了 – Jackson

相關問題