Xamarin Forms - 在工具栏项中显示取消按钮而不是返回 (iOS)

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

我想将 iOS 上 NavigationBar 中的标准 Back 按钮更改为 iOS 中 "New contact" 屏幕中的 Cancel 按钮=].
我正在使用 Xamarin Forms.

编辑:

XAML 模态

<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>

在前面的屏幕中隐藏代码以打开模态

async Task OpenModal()
{
    var page = new NavigationPage(new MyModalView ());
    await App.Current.Navigation.PushModalAsync (page);
}

在进行导航推送的页面的构造函数中执行以下操作。这将适用于推送到堆栈的所有页面。

NavigationPage.SetBackButtonTitle(this, "Cancel");

当然这是 ContentPage(或任何类型的页面)

完成您的请求的标准约定是push a Modal and use ToolBarItems. You can find an example of applying a ToolBarItem to your page on the Xamarin Forums

如果您需要更具体的示例,请告诉我。


更新了示例

两个ToolbarItem像这样:

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

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

现在您可以将这些添加到您的视图中:

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

您甚至可以绑定 CommandProperty:

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

或者简单地处理用户点击项目时的事件:

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

记得wrap your Modal in a NavigationPage,否则工具栏项将不会出现。

希望对您有所帮助。