Xamarin表单-工具栏项目中的“显示取消"按钮,而不是“后退"(iOS) [英] Xamarin Forms - Show Cancel button in toolbar items instead of Back (iOS)

查看:117
本文介绍了Xamarin表单-工具栏项目中的“显示取消"按钮,而不是“后退"(iOS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将iOS上NavigationBar中的标准Back按钮更改为Cancel按钮,例如iOS中的新建联系人"屏幕.
我正在使用Xamarin Forms.

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.

模式的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>

在上一个屏幕中隐藏代码以打开模式

Code-behind in the prior screen to open modal

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

推荐答案

完成请求的标准约定是 ToolBarItems .您可以在 Xamarin论坛上找到将ToolBarItem应用于页面的示例..

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.

示例更新

两个ToolbarItems想要这样:

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

您甚至可以绑定CommandProperty:

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

请记住将模态包装在NavigationPage中,否则ToolbarItems不会出现.

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

希望这会有所帮助.

这篇关于Xamarin表单-工具栏项目中的“显示取消"按钮,而不是“后退"(iOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆