更改主详细信息导航 xamarin 表单中的汉堡包图标 [英] Change hamburger icon in master detail navigation xamarin forms
问题描述
我正在处理 Xamarin 表单,我需要在成功登录屏幕后显示主详细信息导航.我想更改默认的汉堡包图标,但无法更改.
I am working on Xamarin forms where I need to show master detail navigation after successful login screen. I want to change default hamburger icon but not able to change it.
请看下面我使用的代码.
Please see below code I am using.
由于我的应用程序有登录屏幕,所以我不想在登录屏幕上显示任何导航.我只是在 app.xaml.cs
Since my app have login screen so I don't want to show any navigation on Login screen. I am just setting main page in app.xaml.cs
public App()
{
InitializeComponent();
MainPage = new Login();
}
现在点击登录后我尝试了以下方法来更改图标但没有用
Now after login clicked I tried following approach to change icon but didn't work
var dashboard = new Dashboard(){Icon = "Menuicon.png" };
Application.Current.MainPage = dashboard;
Dashbaord
是 masterdetail 页面,在其 ctor 上,我正在设置如下所示的详细信息页面
Dashbaord
is masterdetail page and on its ctor, I am setting detail page like below
Detail = new NavigationPage((Page)Activator.CreateInstance(typeof(DashbaordDetail))) { Icon = "Menuicon.png" };
它没有反映新图标
推荐答案
您应该使用自定义渲染器.
You should use a custom renderer.
在您的 Android 项目中,如下所示:
In your Android project, like this:
[assembly: ExportRenderer(typeof(CustomIcon.Views.MainPage), typeof(IconNavigationPageRenderer))]
namespace CustomIcon.Droid
{
public class IconNavigationPageRenderer : MasterDetailPageRenderer
{
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 ImageButton;
var drawerArrow = imageButton?.Drawable as DrawerArrowDrawable;
if (drawerArrow == null)
continue;
imageButton.SetImageDrawable(Forms.Context.GetDrawable(Resource.Drawable.newIcon));
}
}
}
}
}
在您的 iOS 项目中,仅使用与您的 PCL 项目中的 xaml 文件相同的图标,如下所示:
In your iOS project only use the same icon from you xaml file in your PCL project, like this:
<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:CustomIcon.Views;assembly=CustomIcon"
Title="MainPage"
Icon="newIcon.png"
x:Class="CustomIcon.Views.MainPage">
<MasterDetailPage.Master>
<local:MasterPage x:Name="masterPage" />
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<NavigationPage>
<x:Arguments>
<local:Page1 />
</x:Arguments>
</NavigationPage>
</MasterDetailPage.Detail>
有关更多信息,请参阅我在 github 上的存储库:https://github.com/wilsonvargas/CustomIconNavigationPage
For more information see my repo on github: https://github.com/wilsonvargas/CustomIconNavigationPage
这篇关于更改主详细信息导航 xamarin 表单中的汉堡包图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!