Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
960 views
in Technique[技术] by (71.8m points)

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

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 am using Xamarin Forms.

EDIT:

XAML of modal

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             x:Class="Xrm.Mca.Views.MyModalView">

    <ContentPage.ToolbarItems>
            <ToolbarItem x:Name="Cancel" Text="Cancel" ></ToolbarItem>  
            <ToolbarItem x:Name="Save" Text="Save" ></ToolbarItem>  
    </ContentPage.ToolbarItems>

    <ContentPage.Content>

        <TableView Intent="Form">
            <TableRoot>
                <TableSection Title="Details">
                    <EntryCell Label="Name" Placeholder="Entry your name" />
                    <EntryCell Label="Age" Placeholder="Entry your age" />
                </TableSection>
            </TableRoot>
        </TableView>

    </ContentPage.Content>

</ContentPage>

Code-behind in the prior screen to open modal

async Task OpenModal()
{
    var page = new NavigationPage(new MyModalView ());
    await App.Current.Navigation.PushModalAsync (page);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...