2

i just googled very post but no useful article found. i have one SmartEntry application that have serval modules. one of them first dashboard page have nevigation button and last header row which was title + imagebuttons.

take a look

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SmartEntry.DashBoard"
             Title="Dashboard"
             BackgroundColor="White">

    <ContentPage.ToolbarItems>
        <ToolbarItem Text="Dashboard" Order="Primary" Priority="0"></ToolbarItem>
        <ToolbarItem Icon="logout_icon.png" Order="Primary" Priority="1" Clicked="ToolbarItem_Clicked"/>
    </ContentPage.ToolbarItems>

    <ContentPage.Content>
        <StackLayout>
            <Frame BackgroundColor="White" HasShadow="False">
                <StackLayout Orientation="Vertical" Spacing="10" WidthRequest="100">

......

now i just want this Dashboard title to left align how i achieve this. screen:

enter image description here

how to get DASHBOARD align to left....

Gajjar Shalin
  • 95
  • 1
  • 11
  • 2
    I'm not sure if this is exactly what you want, but I think you can solve it with `TitleView` https://stackoverflow.com/questions/52617416/how-to-add-toolbaritem-on-the-left-side-of-navigationbar-in-xamarin-forms-on-and – fmaccaroni Nov 24 '21 at 14:11
  • Agreed, the [Title View](https://learn.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/navigation-titleview/) is the way to go. It'll also allow you easily change the layout in the future if needed. – Andrew Nov 24 '21 at 20:15

1 Answers1

1

As mentioned above, you can use titleview, or you can create a custom renderer for NavigationPage instead of Page , and override the OnLayout method . Related Document:https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/

Android will change the detault icon back in UpdateToolbar method , and OnLayout method is triggered every time while current page is changed.

On Android:

[assembly: ExportRenderer(typeof(NavigationPage), typeof(MyNavigationRenderer))]
namespace FormsApp.Droid
{
    public class MyNavigationRenderer : NavigationPageRenderer
    {
        Context _context;

        AndroidX.AppCompat.Widget.Toolbar _toolbar;

        public MyNavigationRenderer(Context context) : base(context)
        {
            _context = context;
        }

        public override void OnViewAdded(Android.Views.View child)
        {
            base.OnViewAdded(child);
            if (child.GetType() == typeof(AndroidX.AppCompat.Widget.Toolbar))
            {
                _toolbar = (AndroidX.AppCompat.Widget.Toolbar)child;
                _toolbar.SetNavigationIcon(Resource.Drawable.bbutton_nav);
            }
        }

        protected override void OnLayout(bool changed, int l, int t, int r, int b)
        {
            base.OnLayout(changed, l, t, r, b);

            if (_toolbar != null)
            {
                if (_toolbar.NavigationIcon != null)
                {
                    _toolbar.NavigationIcon = AndroidX.Core.Content.ContextCompat.GetDrawable(_context, Resource.Drawable.bbutton_nav);
                }
            }
        }
    }
}

On Ios:

[assembly: ExportRenderer(typeof(NavigationPage), typeof(MyRenderer))]
namespace FormsApp.iOS
{
    class MyRenderer : NavigationRenderer
    {
        public override void ViewDidLayoutSubviews()
        {
            base.ViewDidLayoutSubviews();

          
            if (this.NavigationBar.TopItem.BackBarButtonItem == null)
            {
                this.NavigationBar.BackIndicatorImage = UIImage.FromFile("pic.png");
                this.NavigationBar.BackIndicatorTransitionMaskImage = UIImage.FromFile("pic.png");
                this.NavigationBar.TopItem.BackBarButtonItem = new UIBarButtonItem("Dashboard", UIBarButtonItemStyle.Plain, null);
            }
        }

    }
}
Adrain
  • 1,946
  • 1
  • 3
  • 7
  • what about this error Severity `Error CS0117 'Resource.Drawable' does not contain a definition for 'bbutton_nav' SmartEntry.Android ' – Gajjar Shalin Nov 25 '21 at 07:36
  • 1
    The file's build action should be "AndroidResource" not "AndroidAsset". – Adrain Nov 25 '21 at 08:09
  • can we chnage Title according to page run in backgroud. – Gajjar Shalin Nov 25 '21 at 09:29
  • i tried but this error occurs 'Severity Code Description Project File Line Suppression State Error invalid file path 'C:\Users\Admin\source\repos\SmartEntry\SmartEntry\SmartEntry.Android\obj\Debug\100\res\mainactivity.cs'. SmartEntry.Android ' – Gajjar Shalin Nov 26 '21 at 10:32
  • i also chnage AndroidResurce but there is another error occurs invalid path mainactivity.cs file – Gajjar Shalin Nov 29 '21 at 05:45