Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Navigation Bar Title and Icon in code

Hy guys, I'm doing a Xamarin.Forms app, as you can see this is my app now:

BottomBarPage

In this screenshot, you can see my App.xaml.cs, where I upload StartPage() which is a BottomBarPage.

public App()
{

            InitializeComponent();

            //MainPage = new Login();

            NavigationPage nav = new NavigationPage(new StartPage());



            Image img = new Image();
            img.Source = "about_us.png";
            Label label = new Label();
            label.Text = "My App";
            label.VerticalTextAlignment = TextAlignment.Center;
            label.TextColor = Color.Black;

            StackLayout stack = new StackLayout();
            stack.Children.Add(img);
            stack.Children.Add(label);


            nav.SetValue(NavigationPage.TitleViewProperty, stack);
            //nav.SetValue(NavigationPage.TitleProperty, stack);
            nav.SetValue(NavigationPage.BarBackgroundColorProperty, Color.FromHex("#D60000"));
            MainPage = nav;


}

As you can see in my first screen, in the App() I'm trying to add to the navigation bar a title and the app icon but doesn't work, what should I do to add it?

like image 603
notarealgreal Avatar asked Jan 14 '19 10:01

notarealgreal


1 Answers

Since Xamarin.Forms 3.2.0 you could place the following layout in StartPage.xaml :

<NavigationPage.TitleView>
    <StackLayout Orientation="Horizontal" BackgroundColor="#D60000">
        <Image Source="about_us.png" />
        <Label Text="My App" VerticalTextAlignment="Center"/>
    </StackLayout>
</NavigationPage.TitleView>
like image 85
Leo Zhu - MSFT Avatar answered Oct 15 '22 13:10

Leo Zhu - MSFT