Button in the action bar for the Xamarin.Forms application?

Is there a way for Xamarin.Forms to add a button to the action bar / navigation element (without resorting to a specific platform code)?

+5
source share
2 answers

If by the action / navigation bar you mean the navigation bar at the top, you can use this method:

private void ShowToolbar() { if (Device.OS == TargetPlatform.iOS) { // move layout under the status bar this.Padding = new Thickness(0, 20, 0, 0); toolbarItem = new ToolbarItem("Sync", "sync_icon.png", () => { //if (!response) //{ // response = true; SyncService(); //} //else // return; }, 0, 0); ToolbarItems.Add(toolbarItem); } if (Device.OS == TargetPlatform.Android) { toolbarItem = new ToolbarItem("Sync", "sync_icon.png", () => { //if (!response) //{ SyncService(); //} //else // return; }, 0, 0); ToolbarItems.Add(toolbarItem); } if (Device.OS == TargetPlatform.WinPhone) { toolbarItem = new ToolbarItem("Sync", "sync_icon.png", () => { //if (!response) //{ // response = true; SyncService(); //} //else // return; }, 0, 0); ToolbarItems.Add(toolbarItem); } } 
+10
source

In MainPage.xaml you can add the following code.

 <ContentPage.ToolbarItems> <ToolbarItem Text="Click Me!" Icon="iconName.png" Clicked="ToolbarItem_Clicked"/> </ContentPage.ToolbarItems> 

Now in MainPage.xaml.cs you should add a click handler.

 public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); } private async void ToolbarItem_Clicked(object sender, EventArgs e) { await Navigation.PushAsync(new NewPage()); } } 

For navigation, the constructor in App.xaml.cs should contain the following code.

 public App() { InitializeComponent(); MainPage = new NavigationPage(new MainPage()); } 
0
source

All Articles