在主要详细信息导航xamarin表单中更改汉堡包图标 [英] Change hamburger icon in master detail navigation xamarin forms

查看:67
本文介绍了在主要详细信息导航xamarin表单中更改汉堡包图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆