Xamarin Forms - Show Cancel button in toolbar items instead of Back (iOS)

后端 未结 2 1883
失恋的感觉
失恋的感觉 2020-12-21 04:35

I want to change the standard Back button in the NavigationBar on iOS to a Cancel button like the \"New contact\" screen in iOS.
I

相关标签:
2条回答
  • 2020-12-21 04:55

    Do the following in the constructor of the page doing the navigation push. This will work for all pages pushed on to the stack.

    NavigationPage.SetBackButtonTitle(this, "Cancel");
    

    Where this is the ContentPage (or any type of page) of course

    0 讨论(0)
  • 2020-12-21 05:12

    The standard convention of accomplishing your request is to push a Modal and use ToolBarItems. You can find an example of applying a ToolBarItem to your page on the Xamarin Forums.

    Let me know if you need a more concrete example.


    UPDATED WITH EXAMPLE

    The two ToolbarItems would like like so:

    var cancelItem = new ToolbarItem
    {
        Text = "Cancel"
    };
    
    var doneItem = new ToolbarItem
    {
        Text = "Done"
    };
    

    Now you can add these to your view:

    this.ToolbarItems.Add(cancelItem);
    this.ToolbarItems.Add(doneItem);
    

    You can even bind the CommandProperty:

    doneItem.SetBinding(MenuItem.CommandProperty, "DoneClicked");
    

    Or simply handle the event when the user taps the item:

    doneItem.Clicked += (object sender, System.EventArgs e) => 
    {
        // Perform action
    };
    

    Remember to wrap your Modal in a NavigationPage, as the ToolbarItems otherwise will not appear.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题