Xamarin表示不在视图上显示的工具栏项

时间:2014-08-25 22:32:16

标签: c# android xamarin

我有许多视图页面都继承自ContentPage。被调用的第一页 从app.cs正确显示工具栏中的图标。第二页(TodoItemView)根本不显示图标(使用相同的代码)。我是否需要做一些事情来初始化第二个视图的工具栏?

提前感谢。

This page is called by the following code:

      var todoList = new TodoListView();
      return new NavigationPage(todoList);

Th page is called by the following code:

      var todoList = new TodoListView();
      return new NavigationPage(todoList);




// Works shows refresh icon
public class TodoListView : ContentPage
    {
        public TodoListView ()
        {
            Title = "Social Events";

            // Display the refresh icon
            var toolbarItem = new ToolbarItem ("Refresh", "refresh.png", ()=>{
            }, 0, 0);
            ToolbarItems.Add (toolbarItem);

            var list = new ListView ();
            var viewModel = new TodoListViewModel ();
            list.ItemsSource = viewModel.TodoList;

            var cell = new DataTemplate (typeof(AspectImageCell));
            cell.SetBinding (TextCell.TextProperty, "Name");
            cell.SetBinding (TextCell.DetailProperty, "Details");
            cell.SetBinding (ImageCell.ImageSourceProperty, "Image");
            list.ItemTemplate = cell;
            list.ItemTapped += (sender, args) => {


                Navigation.PushAsync (new NavigationPage(new TodoItemView ()));

                // Reset the selected item
                list.SelectedItem = null;
            };

            Content = list;
        }
    }

// Does not work, does not show icon
public class TodoItemView : ContentPage
    {
        public TodoItemView ()
        {
            Title = "Todo item";

            var toolbarItem = new ToolbarItem ("Send", "send.png", ()=>{

                // email todo item

            }, 0, 0);
            ToolbarItems.Add (toolbarItem);
         }
}

3 个答案:

答案 0 :(得分:7)

如果您的应用没有工具栏,则不会显示您的ToolbarItems。添加一个的最简单方法是将页面包装在NavigationPage

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

答案 1 :(得分:2)

您只需要为每个导航堆栈创建一次NavigationPage。当你按下TodoItemView时,请改为:

list.ItemTapped += (sender, args) => {
  Navigation.PushAsync (new TodoItemView ());
  // Reset the selected item
  list.SelectedItem = null;
};

TodoItemView将被推送到第一个NavigationPage中已包含的导航堆栈。消除额外的NavigationPage也可能会修复您丢失的工具栏项。如果没有,还要验证您指定的工具栏图标是否有效。

答案 2 :(得分:0)

添加页面的最简单方法是将页面包装在NavigationPage对象中,然后像这样调用它:

var testPage = new NavigationPage(new TestPage());
Navigation.PushAsync(testPage);