更改导航栏上的图标 - Xamarin.Forms Android

时间:2016-08-19 21:58:33

标签: c# xamarin xamarin.android xamarin.forms

如何在Xamarin.Forms中设置不同的图标 - android。

一个用于导航页面的应用程序,播放商店,用户屏幕等。

我更新了Project.Droid / MainActivity.cs文件:

[Activity(Label = "MyAppName", Icon =  "MyIconName", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]

但是这样改变了两个图标!!

我做的其他方式,我更新了ProjectForms / App.cs:

        Current.Resources = new ResourceDictionary();
        Current.Resources.Add("UlycesColor", Color.FromRgb(121, 248, 81));
        var navigationStyle = new Style(typeof(NavigationPage));
        var barTextColorSetter = new Setter { Property = NavigationPage.BarTextColorProperty, Value = Color.Black };
        var barBackgroundColorSetter = new Setter { Property = NavigationPage.BarBackgroundColorProperty, Value = Color.White };
        var barIcon = new Setter { Property = NavigationPage.IconProperty, Value = "newIcon.png" };

        navigationStyle.Setters.Add(barTextColorSetter);
        navigationStyle.Setters.Add(barBackgroundColorSetter);
        navigationStyle.Setters.Add(barIcon);
        Current.Resources.Add(navigationStyle);

但是不起作用!!

任何想法???

1 个答案:

答案 0 :(得分:1)

您可以使用自定义渲染器执行此操作:

[assembly:ExportRenderer (typeof(NavigationPage), typeof(NavigationPageRenderer))]
namespace SuperForms.Samples.Droid
{
    public class NavigationPageRenderer : NavigationRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<NavigationPage> e)
        {
            base.OnElementChanged(e);

            var actionBar = ((Activity)Context).ActionBar;
            actionBar.SetIcon(Resource.Drawable.newIcon);
        }
    }
}

将图标添加到Resources/drawable文件夹:

enter image description here

它的外观:

enter image description here