gpt4 book ai didi

android - 如何更改 xamarin 表单中的导航页面后退按钮

转载 作者:太空狗 更新时间:2023-10-29 16:27:22 25 4
gpt4 key购买 nike

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

    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;
}
}

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

但是,当我启动我的应用程序时,我有相同的标准箭头图标 enter image description here

最佳答案

如果你的MainActivityFormsApplicationActivity,你可以引用这个例子:

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

如果您的 MainActivity 类型是 FormsAppCompatActivity,您可以自定义一个 PageRenderer 并更改 Toolbar导航图标

例如:

[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());
}

Effect .


更新:

当你使用Navigation.PushAsync()方法导航到另一个页面时,系统会自动更新Toolbar的图标,你可以在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的图标。

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());
}

关于android - 如何更改 xamarin 表单中的导航页面后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47323343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com