xmarin.android导航栏,android – 如何在xamarin表单中更改导航页面后退按钮

我想在导航页面中更改后退箭头图像.为此在Android应用程序中我创建了导航页面渲染器,然后使用方法toolbar.SetNavigationIcon和它不工作,但当我使用toolbar.SetLogo其工作正常.

protected override void OnElementChanged(ElementChangedEventArgs 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;

}

}

我还尝试将图像添加到app:navigationIcon中的apps.con,它在设计器中显示效果很好

my arrow

解决方法:

如果您的MainActivity是FormsApplicationActivity,您可以参考此示例:

如果您的MainActivity类型是FormsAppCompatActivity,则可以自定义PageRenderer并更改工具栏的NavigationIcon.

例如 :

[assembly: ExportRenderer(typeof(ContentPage), typeof(NavigationPageRendererDroid))]

...

public class NavigationPageRendererDroid : PageRenderer

{

protected override void OnElementChanged(ElementChangedEventArgs e)

{

base.OnElementChanged(e);

var context = (Activity)Xamarin.Forms.Forms.Context;

var toolbar = context.FindViewById(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()方法导航到另一个页面时,系统将自动更新工具栏的图标,您可以在source code中找到:

protected virtual Task OnPushAsync(Page view, bool animated)

{

return SwitchContentAsync(view, animated);

}

Task 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方法来设置工具栏的图标.

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 OnPushAsync(Page view, bool animated)

{

var retVal = base.OnPushAsync(view, animated);

context = (Activity)Xamarin.Forms.Forms.Context;

toolbar = context.FindViewById(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());

}

标签:android,xamarin,xamarin-forms,toolbar,navigation

来源: https://codeday.me/bug/20191001/1838079.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值