How to add an icon in navigation bar for navigation page in xamarin forms for android?

走远了吗. 提交于 2019-12-26 09:00:11

问题


I have made simple navigation pages. Now I want to add icon for android in navigation bar.

I added an screenshot and highlighted with black circle where i want to add an icon.

I also tried custom renderer.
That is provided answer from this post: Change icon on Navigation bar - Xamarin.Forms Android

But not worked for me.
Here it is


回答1:


[assembly: ExportRenderer(typeof(NavigationPage), typeof(CustomMapRenderer))]
namespace XamarinFormsMaps.Droid
{
    public class CustomMapRenderer : NavigationPageRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<NavigationPage> e)
        {
            base.OnElementChanged(e);
            var bar = (Android.Support.V7.Widget.Toolbar)typeof(NavigationPageRenderer)
            .GetField("_toolbar", BindingFlags.NonPublic | BindingFlags.Instance)
            .GetValue(this);
            bar.SetLogo(Resource.Drawable.icon);
        }
    }
}

Try this one!




回答2:


I personally haven't used it but I guess you are looking for this:

NavigationPage.SetTitleIcon (this, "image.png");

This will set the title Icon in Xamarin Forms. If you want to change the back button Icon, hamalaiv gave you this link




回答3:


Thank you Dilmah.

It work for me. With Xamarin.Forms 2.5, I change the constructor by

public CustomMapRender(Context context) : base(context)

to avoid warning of obsolete constructor.



来源:https://stackoverflow.com/questions/43466050/how-to-add-an-icon-in-navigation-bar-for-navigation-page-in-xamarin-forms-for-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!