更改汉堡菜单图标 - Xamarin.Forms Android 和 UWP
对于 UWP 部分,您可以轻松替换汉堡图标。因为在 UWP 平台上,它是用带有样式的按钮PaneButton渲染的,你可以在这里找到它。只需将其复制到 UWP App.xaml文件并替换 content 属性,如下所示。
<Application.Resources>
<ResourceDictionary>
<Style x:Key="PaneButton" TargetType="Button">
<Setter Property="FontFamily" Value="{ThemeResource SymbolThemeFontFamily}" />
<Setter Property="FontSize" Value="20" />
<Setter Property="Height" Value="48" />
<Setter Property="Width" Value="48" />
<Setter Property="Content" Value="" />
</Style>
</ResourceDictionary>
</Application.Resources>
对于 Android 部分,我们需要自定义MasterDetailPageRenderer渲染,如下所示
[assembly: ExportRenderer(typeof(MainPage), typeof(IconNavigationPageRenderer))]
namespace masterDe.Droid
{
public class IconNavigationPageRenderer : MasterDetailPageRenderer
{
public IconNavigationPageRenderer(Context context):base(context) { }
private static Android.Support.V7.Widget.Toolbar GetToolbar() => (CrossCurrentActivity.Current?.Activity as MainActivity)?.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
base.OnLayout(changed, l, t, r, b);
var toolbar = GetToolbar();
if (toolbar != null)
{
for (var i = 0; i < toolbar.ChildCount; i++)
{
var imageButton = toolbar.GetChildAt(i) as Android.Widget.ImageButton;
var drawerArrow = imageButton?.Drawable as DrawerArrowDrawable;
if (drawerArrow == null)
continue;
// replace default hamburg icon
imageButton.SetImageDrawable(Forms.Context.GetDrawable(Resource.Drawable.icon));
}
}
}
}
}