使用Xamarin表单在每个页面上显示不同的工具栏按钮 [英] Showing different toolbar buttons on each page with Xamarin Forms

查看:67
本文介绍了使用Xamarin表单在每个页面上显示不同的工具栏按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Xamarin Forms应用程序中有2个页面.我的首页在工具栏中有4个图标.我的第二页是登录页,在工具栏中有一个勾号和一个叉号.

I have 2 pages in my Xamarin Forms app. My first page has 4 icons in the toolbar. My second page is a login page and has a tick and a cross in the toolbar.

除非将其设置为导航页面,否则我无法使登录页面显示任何图标.在调用PushAsync()之前,我还必须先清除首页上的ToolBarItems,否则它会抱怨工具栏项太多.

I can't get the login page to show any icons unless I make it a navigation page. I also have to clear ToolBarItems on the first page before calling PushAsync() otherwise it complains there are too many toolbar items.

如果我在登录页面上调用PopAsync(),它不会返回第一页.我猜这是由于它们是2个导航页面.我还尝试了PopToRootAsync().但是,后退按钮仍然有效.

If I call PopAsync() on the login page it does not return to the first page. I'm guessing this is due to their being 2 navigation pages. I also tried PopToRootAsync().The back button works however.

我的问题是-如何在2个不同的页面上显示不同的工具栏图标,以允许导航正常工作?

My question is - how do I show different toolbar icons on 2 different pages in a way that allows navigation to work?

我正在Windows Phone 8.0上对此进行测试

I'm testing this on Windows Phone 8.0

这是调用登录页面的代码:

Here is the code calling the login page:

    private async void ShowLoginPage()
    {
        ToolbarItems.Clear();
        var page = new NavigationPage(new LoginPage());
        await Navigation.PushAsync(page);
    }

这是返回首页的代码:

    private void Cancel()
    {
        Navigation.PopToRootAsync();
    }

我正在运行Xamarin.Forms v1.2.2.6243

I'm running Xamarin.Forms v1.2.2.6243

推荐答案

您有一个选项,一个我在自己的应用中实现的选项,是一个自定义渲染器,可从应用中删除导航标题,然后您可以构建您自己的自定义标题.使用这种方法,您确实会失去应用程序的某些本机感觉,并且您必须自己实现许多过渡功能.但是,它使您对外观有了更多的控制.

One option that you have, and one that I implemented in my own app, is a custom renderer that removes the navigation header from the app and then you could build your own custom header. With this approach, you do lose some of the native feel of the app, and you have to implement much of the transitional functionality your self. However, it gives you alot more control over the look.

删除导航栏的CustomRenderer:

CustomRenderer that removes the navigationBar:

//add using statements

// add all view here that need this custom header, might be able to build a 
//base page that others inherit from, so that this will work on all pages.
[assembly: ExportRenderer(typeof(yourView), typeof(HeaderRenderer))] 

class HeaderRenderer : PageRenderer
{
    public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated);
        this.NavigationController.SetNavigationBarHidden(true, true);
    }
}

在此之后,您可以构建一个标题视图,该标题视图可以放置在每个页面的顶部(我正在使用xaml),因此我不知道它是否与您的应用程序相关.

After this you can build a header view that can be placed on the top of every page (I am using xaml) so I don't know if it is relevant in you application.

您可能需要针对不同的页面类型更改此渲染器.

You might need to change this renderer for differnt page types.

这篇关于使用Xamarin表单在每个页面上显示不同的工具栏按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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