Toolbar item not showing in xamarin forms

匿名 (未验证) 提交于 2019-12-03 02:05:01

问题:

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.

回答1:

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 ()); } 


回答2:

Just wanted to add an edge case to this which drove me nuts for a couple of hours. I had been adding icons outside of Visual Studio and attempting to reference them as toolbar icons in my Xamarin forms app. When testing the Android app, the toolbar icon I was referencing wouldn't display. I finally figured out that the icons weren't being packaged at all in the .APK file. Why? I'd forgotten to include them in the Visual Studio project. Solution Explorer -> Click on project -> Click on Show All Files icon in header bar of Solution Explorer. Icon files then appear, so just right-click them and select Include in Project for each one. Doh...gnash...gnash etc.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!