2016-08-19 23 views
1

如何在Xamarin.Forms - android中設置不同的圖標。更改導航欄上的圖標 - 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

您可以用自定義渲染做到這一點:

[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

+0

優秀。這是正確的方法。非常感謝!!!! – hagnr

+0

如何在ios中執行此操作? –