在代码中设置导航栏标题和图标

Set Navigation Bar Title and Icon in code

大家好,我正在做一个 Xamarin.Forms 应用程序,你现在可以看到这是我的应用程序:

BottomBarPage

在此屏幕截图中,您可以看到我的 App.xaml.cs,我在其中上传 StartPage(),它是一个 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;


}

正如您在我的第一个屏幕中看到的,在 App() 中,我试图向导航栏添加标题和应用程序图标,但不起作用,我应该如何添加它?

从 Xamarin.Forms 3.2.0 开始,您可以在 StartPage.xaml 中放置以下布局:

<NavigationPage.TitleView>
    <StackLayout Orientation="Horizontal" BackgroundColor="#D60000">
        <Image Source="about_us.png" />
        <Label Text="My App" VerticalTextAlignment="Center"/>
    </StackLayout>
</NavigationPage.TitleView>