问题
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