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

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

问题描述

我的 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);
    }

这里是返回第一页的代码:

and here is the code to return to the first 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 Forms 在每个页面上显示不同的工具栏按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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