简体   繁体   English

Xamarin Android工具栏如何动态添加项目

[英]Xamarin Android toolbar how to add items dynamically

I have implemented android toolbar as per the instructions given in xamarin android guides here 我已经实现Android的工具栏按在xamarin Android指南的指示在这里

using example code: 使用示例代码:

public override bool OnCreateOptionsMenu(IMenu menu)
        {           
            MenuInflater.Inflate(Resource.Menu.mainMenu, menu);

            return base.OnCreateOptionsMenu(menu);
        }

and xml : 和xml:

<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:local="http://schemas.android.com/apk/res-auto">
  <item
       android:id="@+id/menu_share"
       local:showAsAction="ifRoom"
       android:title="Share" />
  <item
       android:id="@+id/menu_settings"
       local:showAsAction="ifRoom"
       android:title="Settings" />
</menu>

Now i want to add items to toolbar that will come from my web api , they are basically links to our pages such as privacy policy etc. but i cant find any example on menu.Add method that does this. 现在我想将项目添加到来自我的Web api的工具栏中,它们基本上是指向我们页面的链接,例如隐私权政策等。但是我找不到在menu.Add方法上执行此操作的任何示例。 basically what i need is to constrcut meny items from my json which returns: 基本上我需要从返回的json中构造大量项:

[{
pageid:1,
pagename:"home",
pagehtmllink:"....."
},
{
pageid:2,
pagename:"About",
pagehtmllink:"....."
},
...........
]

how to achieve this from code behind? 如何从后面的代码实现呢?

IMenu has an Add method that you can call: IMenu具有您可以调用的Add方法:

public override bool OnCreateOptionsMenu(IMenu menu)
{
        MenuInflater.Inflate(Resource.Menu.main_menu, menu);

        menu.Add(0, 99, 0, "DB Copy to SDCard (Debug)");

        return base. OnCreateOptionsMenu(menu);
}

For the parameters, review the Android docs: Menu.html#add 有关参数,请查看Android文档: Menu.html#add

To capture the menu selection, you can check the IMenuItem.ItemId value: 要捕获菜单选择,可以检查IMenuItem.ItemId值:

public override bool OnOptionsItemSelected(IMenuItem item)
{
    switch (item.ItemId)
    {
        case 99:
            DoSomething();
            return true;
        default:
            return false;
    }
}

You dont need that you can create a tabbed page and on each page you create a webview 您不需要创建选项卡式页面,并且在每个页面上创建一个Webview

public partial class MainPage : TabbedPage
{
    public MainPage()
    {
        InitializeComponent();
        this.BarBackgroundColor = Color.Gray;

        var homePage = new HomePage();
        var accountPage = new AccountPage();
        var ordersPage = new OrdersPage();
        var promotionsPage = new PromotionsPage();
        var searchPage = new SearchPage();

        homePage.Icon = "imgs_main.png";
        accountPage.Icon = "imgs_account.png";
        ordersPage.Icon = "imgs_orders.png";
        promotionsPage.Icon = "imgs_promotions.png";
        searchPage.Icon = "imgs_search.png";

        homePage.Title = "Home";
        accountPage.Title = "Account";
        ordersPage.Title = "Orders";
        promotionsPage.Title = "Promotions";
        searchPage.Title = "Search";

        Children.Add(homePage);
        Children.Add(accountPage);
        Children.Add(ordersPage);
        Children.Add(promotionsPage);
        Children.Add(searchPage);

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM