如何在Xamarin Forms中更改Navigastion的页面箭头? [英] How could I change the Navigastion's page arrow in Xamarin Forms?

查看:67
本文介绍了如何在Xamarin Forms中更改Navigastion的页面箭头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用xamarin Forms(多平台)创建应用,正在使用导航页面,但是我想将 arrow (<-")更改为 text (后退")

I'm creating an app using xamarin Forms (multiplatform), I'm using a Navigation page, but I want to change the arrow ("<-") to text ("back")

你知道我该怎么做吗?谢谢

Do you know how could i do it? Thanks

(我将在Android应用中使用它,但我将使用Xamarin表单创建该应用)

(I'm going to use it in an Android App, but I'm creating the app using Xamarin forms)

推荐答案

您可以使用自定义渲染器删除导航图标并为其设置文本.但是,这样做时,您需要捕获文本的点击并模拟back事件.

You could use custom renderer to remove the navigation icon and set it with text. But, when you do that, you need to capture the click of the text and simulate the back event.

创建界面:

public class CustomNavigationPage : NavigationPage
{
    public CustomNavigationPage(Page startupPage) : base(startupPage)
    {

    }
}

Android的实现:

The implementation of Android:

  [assembly: ExportRenderer(typeof(CustomNavigationPage), 
typeof(NavigationPageRenderer_Droid))]
namespace NavigationPageDemo.Droid
{
public class NavigationPageRenderer_Droid : NavigationPageRenderer
{
    public Android.Support.V7.Widget.Toolbar toolbar;
    public Activity context;
    public NavigationPageRenderer_Droid(Context context) : base(context)
    {

    }
    protected override Task<bool> OnPushAsync(Page view, bool animated)
    {
        var retVal = base.OnPushAsync(view, animated);

        context = (Activity)Forms.Context;
        toolbar = context.FindViewById<Android.Support.V7.Widget.Toolbar>(Droid.Resource.Id.toolbar);

        if (toolbar != null)
        {
            //if (toolbar.NavigationIcon != null)
            //{
            //toolbar.NavigationIcon = Android.Support.V7.Content.Res.AppCompatResources.GetDrawable(context, Resource.Drawable.back);
            //toolbar.NavigationIcon = null;

            toolbar.NavigationIcon = null;
            toolbar.Title = "back";
            toolbar.SetOnClickListener(new OnClick());
        


            //}
        }

        return retVal;
    }

    protected override Task<bool> OnPopViewAsync(Page page, bool animated)
    {
        return base.OnPopViewAsync(page, animated);
    }

}
public class OnClick : Java.Lang.Object, IOnClickListener
{
    void IOnClickListener.OnClick(Android.Views.View v)
    {
        App.Current.MainPage.Navigation.PopAsync();
    }

  
}

在自定义渲染器中,使用 OnClickListener 捕获对文本的点击.

In the custom renderer, use the OnClickListener to capture the click on text.

这篇关于如何在Xamarin Forms中更改Navigastion的页面箭头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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