18

我正在尝试Remove()Clear() ToolbarItems。这是我ToolbarItem在 MainPage.cs 中创建的代码

public partial class MainPage : MasterDetailPage
{
   public ToolbarItem cCounter = new ToolbarItem() { Icon = "picture.png" };
   public ToolbarItem pPo = new ToolbarItem() { Text = "-" };

   public MainPage()
    {
        InitializeComponent();
        if (Device.OS == TargetPlatform.iOS)
        {
            provider.Clicked += iOS_Ppo_Clicked;
            ToolbarItems.Add(cCounter);
            ToolbarItems.Add(pPo);
        }
    }

    private void iOS_Ppo_Clicked(object sender, EventArgs e)
    { 
        OpenWindow();            
    }

    public async void OpenWindow()
    {
        if (await Common.WindowComands.CanOpenWindow<PPoPage>(Detail))
        {  
            page = new PPoPage(Page3.Allproviders);

            this.ToolbarItems.Clear(); // here I am getting error:Index was outside the bounds of the array 

            page.OnSelected += Page_OnSelected;
            await Detail.Navigation.PushAsync(page, false);             
        }   
    }
}

编辑:当我包含this.ToolbarItems.Clear();OpenWindow 初始化另一个页面打开的方法中并且它可以工作时!清除所有工具栏项,但不幸的是显示此错误:

System.IndexOutOfRangeException: Index was outside the bounds of the array.

如您所见,此项目应仅在 iOS 中消失。这是我想要的页面Remove()ToolbarItems

public partial class PPoPage : ContentPage
{
   public MainPage main { get; set; }

   private List<PPo> Pro;

   public PPoPage(List<PPo> po)
   {
      InitializeComponent();
      if (Device.OS == TargetPlatform.iOS)
      {
         Pro = po;
         CreateLayout();
         // HERE I WANT TO REMOVE TOOLBARITEMS FOR THIS PAGE
         this.ToolbarItem.Remove(main.cCounter); // here there is error
         this.ToolbarItems.Clear(); // this also doesn't work, because toolbar items still exist after this initialization.
      }
   }
}

在这堂课中,我尝试了这两种方法,但都没有奏效。感谢您的回答或建议。

4

3 回答 3

1

我的任务是更改工具栏项的可见性,并创建了带有围绕ToolbarItems集合的包装器的基本内容页面。我的样本可以帮助你:

git 上的示例

我在 CodeReview 上发布的一些“教程”

在此处输入图像描述

于 2016-09-06T09:56:37.287 回答
1

首先,我真的要感谢我尝试在我的解决方案中实现这两个答案的所有答案和建议,但不幸的是没有成功。主要原因是我没有专注于实现整个 iOS 工具栏的课程,而是在我的 iOS 项目上。

在 iOS 工具栏上,我只写了简单的 if 语句:

var currentPage = ((NavigationPage)Element).CurrentPage;
if (currentPage != null && currentPage is ContentPage)
{
    TopViewController.NavigationItem.RightBarButtonItems = new UIBarButtonItem[0];
                return;
}
于 2016-09-07T14:22:41.747 回答
0

这里的问题PPoPage是与 不同的页面实例MainPage,因此包含其自己的 ToolbarItems 集合。

尝试main.ToolbarItems.Clear()代替this.ToolbarItems.Clear(),假设main属性已初始化,或者您可以使用 访问顶级当前页面Xamarin.Forms.NavigationPage.CurrentPage

于 2016-09-02T15:34:17.870 回答