Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
692 views
in Technique[技术] by (71.8m points)

ios - Toolbar item not showing in xamarin forms

I am pretty much new to Xamarin forms. I am trying to add toolbar items to content page.

I am using IPAD Air as deployment device and used sever iPhone/ipad simulators as well. I referred the XamarinForms-Mobile App pdf document. Though its a basic, i am not sure, why i am not able to see it in device. Please help me where i am making the mistake.

public partial class SamplePage : ContentPage   
{
    public SamplePage()
    {
        //Toolbar items
            ToolbarItem scanItem = new ToolbarItem ();
            scanItem.Name = "Scan";
            scanItem.Order = ToolbarItemOrder.Primary;

            ToolbarItem settingsItem = new ToolbarItem ();
            settingsItem.Name = "Settings";
            settingsItem.Order = ToolbarItemOrder.Primary;

            ToolbarItems.Add (scanItem);
            ToolbarItems.Add (settingsItem);
    }
}

App.cs

public class App
    {
        public static Page GetMainPage ()
        {   
            return new SamplePage ();
        }
    }

Tried xaml way of doing it as well. But no result. Content page is blank.

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="BLE_Core.BLECorePage">
    <ContentPage.Content>
    </ContentPage.Content>
    <ContentPage.ToolbarItems>
        <ToolbarItem Name = "Scan"></ToolbarItem>
        <ToolbarItem Name = "Settings"></ToolbarItem>
   </ContentPage.ToolbarItems>
</ContentPage>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your ToolbarItems will not show if your app does not have a Toolbar. The simplest way to add one is to wrap your page in a NavigationPage

public static Page GetMainPage ()
{   
  return new NavigationPage(new SamplePage ());
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...