如何在 xamarin 窗体中更改导航页后退按钮



我正在尝试更改导航页面中的后退箭头图像。为此,我在 android 应用程序中创建了导航页面渲染器,然后使用方法工具栏。设置导航图标,它不起作用,但是当我使用工具栏时。设置徽标它工作正常。

    protected override void OnElementChanged(ElementChangedEventArgs<NavigationPage> e)
    {
        base.OnElementChanged(e);
        toolbar.SetNavigationIcon(Resource.Drawable.arrow); 
        toolbar.SetLogo(Resource.Drawable.arrow);
    }
    public override void OnViewAdded(Android.Views.View child)
    {
        base.OnViewAdded(child);
        if (child.GetType() == typeof(Android.Support.V7.Widget.Toolbar))
        {
            toolbar = (Android.Support.V7.Widget.Toolbar)child;
            toolbar.ChildViewAdded += Toolbar_ChildViewAdded;
        }
    }

我还尝试将图像添加到工具栏.axml中的app:navigationIcon,它在设计器中显示很棒我的箭

但是,当我启动我的应用程序时,我有相同的标准箭头图标在此处输入图像描述

如果你的MainActivityFormsApplicationActivity,你可以参考这个例子:

https://github.com/jessejiang0214/ChangeBackIconInXF/tree/master/Droid

如果你的MainActivity类型是 FormsAppCompatActivity ,你可以自定义一个PageRenderer并更改ToolbarNavigationIcon

例如:

[assembly: ExportRenderer(typeof(ContentPage), typeof(NavigationPageRendererDroid))]
...
public class NavigationPageRendererDroid : PageRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Page> e)
    {
        base.OnElementChanged(e);
        var context = (Activity)Xamarin.Forms.Forms.Context;
        var toolbar = context.FindViewById<Android.Support.V7.Widget.Toolbar>(Droid.Resource.Id.toolbar);
        toolbar.NavigationIcon = Android.Support.V4.Content.ContextCompat.GetDrawable(context, Resource.Drawable.Back);
    }
}

用法:

MainPage = new NavigationPage(new MainPage());
...
//When click a button in MainPage, navigate to another page
private async void Button_Clicked(object sender, EventArgs e)
{
    await Navigation.PushAsync(new TestPage());
}

影响。


更新:

当你使用Navigation.PushAsync()方法导航到另一个页面时,系统会自动更新Toolbar的图标,你可以在源代码中找到:

protected virtual Task<bool> OnPushAsync(Page view, bool animated)
{
    return SwitchContentAsync(view, animated);
}
Task<bool> SwitchContentAsync(Page page, bool animated, bool removed = false, bool popToRoot = false)
{
    ...
    UpdateToolbar();
    ...
}
void UpdateToolbar()
{
    ...
    bool isNavigated = ((INavigationPageController)Element).StackDepth > 1;
    if (isNavigated)
    {
        ...
        if (NavigationPage.GetHasBackButton(Element.CurrentPage))
        {
             //Set the navigation back  icon < =================== !!! =-=
             var icon = new DrawerArrowDrawable(activity.SupportActionBar.ThemedContext);
             icon.Progress = 1;
             bar.NavigationIcon = icon;
        }
    }
    ...
}

溶液:

所以我们别无选择,只能自定义一个NavigationPageRenderer,覆盖OnPushAsync方法来设置Toolbar的图标。

using AToolbar = Android.Support.V7.Widget.Toolbar;
[assembly: ExportRenderer(typeof(CustomNavigationPage), typeof(NavigationPageRendererDroid))] // APPCOMP
...
public class NavigationPageRendererDroid : Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer // APPCOMP
{
    public AToolbar toolbar;
    public Activity context;
    protected override Task<bool> OnPushAsync(Page view, bool animated)
    {
        var retVal = base.OnPushAsync(view, animated);
        context = (Activity)Xamarin.Forms.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.V4.Content.ContextCompat.GetDrawable(context, Resource.Drawable.Back);
                //toolbar.SetNavigationIcon(Resource.Drawable.Back);
            }
        }
        return retVal;
    }
}

CustomNavigationPagePCL中定义:

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

用法:

public App()
{
    InitializeComponent();
    MainPage = new CustomNavigationPage(new MainPage());
}
...
// In MainPage
private async void Button_Clicked(object sender, EventArgs e)
{
    await Navigation.PushAsync(new TestPage());
}

我用下一种方式解决了这个问题:在我的主活动中,我添加了静态工具栏属性并在OnCreateOptionsMenu中标识了它

    public static Toolbar ToolBar { get; private set; }
    public override bool OnCreateOptionsMenu(IMenu menu)
    {
        ToolBar = FindViewById<Toolbar>(Resource.Id.toolbar);
        ToolBar.SetNavigationIcon(Resource.Drawable.arrow);
        return base.OnCreateOptionsMenu(menu);
    }
    protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;
        ....
    }

然后在PageRenderer中:

protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
{
     base.OnElementChanged(e);
     MainActivity.ToolBar?.SetNavigationIcon(Resource.Drawable.arrow);
}

但!!从 2 种方式我对重绘有不良影响

用法:

async void tapImage_Tapped(object sender, EventArgs e)
{
     await Navigation.PushAsync(new ChooseGenrePage(_listGenres));
}

相关内容

  • 没有找到相关文章

最新更新