如何更改 xamarin 表单中的导航页面后退按钮 [英] How to change navigation page back button in xamarin forms
问题描述
我正在尝试更改导航页面中的后退箭头图像.为此,我在 android 应用程序中创建了导航页面渲染器,然后使用方法 toolbar.SetNavigationIcon 并且它不起作用,但是当我使用 toolbar.SetLogo 时它工作正常.
I am trying to change back arrow image in navigation page. For this in android app i created navigation page renderer and then using method toolbar.SetNavigationIcon and its not working, but when i use toolbar.SetLogo its working fine.
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;
}
}
我还尝试将图像添加到 barbar.axml 中的 app:navigationIcon,并且在设计器中显示效果很好我的箭头
I also tried add image to app:navigationIcon in toolbar.axml, and it shows great in designer my arrow
但是,当我启动我的应用程序时,我有相同的标准箭头图标在此处输入图片描述
But, when i starting my app i have the same standart arrow icon enter image description here
推荐答案
如果你的 MainActivity
是 FormsApplicationActivity
,你可以参考这个例子:
If your MainActivity
is FormsApplicationActivity
, you could refer to this example :
https://github.com/jessejiang0214/ChangeBackIconInXF/tree/master/Droid
如果您的 MainActivity
类型是 FormsAppCompatActivity
,您可以自定义一个 PageRenderer
并更改 Toolbar
的 <代码>导航图标代码>.
If your MainActivity
type is FormsAppCompatActivity
, you could custom a PageRenderer
and change the Toolbar
's NavigationIcon
.
例如:
[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
的图标,您可以在源代码:
When you use Navigation.PushAsync()
method navigate to another page, the system will automatically update the Toolbar
's icon, you could find in the source code :
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
的图标.
Solution :
So we have no choice but to custom a NavigationPageRenderer
, override the OnPushAsync
method to set the Toolbar
's icon.
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;
}
}
CustomNavigationPage
在 PCL
中定义:
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());
}
这篇关于如何更改 xamarin 表单中的导航页面后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!