18

Remove()またはしようとしていClear() ToolbarItemsます。ToolbarItemMainPage.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 に答える
0

ここでの問題は、PPoPageとは異なるページ インスタンスであるためMainPage、独自の ToolbarItems コレクションが含まれていることです。

プロパティが初期化されていると仮定して、 のmain.ToolbarItems.Clear()代わりに試してみてください。または、 でトップレベルの現在のページにアクセスできます。this.ToolbarItems.Clear()mainXamarin.Forms.NavigationPage.CurrentPage

于 2016-09-02T15:34:17.870 に答える