简体   繁体   English

Xamarin.Forms 项目中不显示页面标题

[英]Page Title doesn't display in Xamarin.Forms project

I am working on Xamarin.Forms.我正在研究 Xamarin.Forms。

When I created blank Xamarin.Forms project there are four project created in one solution, one for iOS, one for Android, one for Windows and one is the Portable project (Common Project).当我创建空白的 Xamarin.Forms 项目时,在一个解决方案中创建了四个项目,一个用于 iOS,一个用于 Android,一个用于 Windows,一个是便携式项目(通用项目)。

I add one XAML form named "Page2.XAML" with this code:我使用以下代码添加了一个名为“Page2.XAML”的 XAML 表单:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Rutul_App.Page2"
             Title="aaaa">
    <ContentPage.Content>
        <StackLayout>
            <Label Text="ABC" HorizontalOptions="Center"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

and in the code behind file:并在文件背后的代码中:

namespace Rutul_App
{
    public partial class Page2 : ContentPage
    {
        public Page2()
        {
            InitializeComponent();
        }
    }
}

In App.cs page I have added:在 App.cs 页面中,我添加了:

public App()
    {
        // The root page of your application
        MainPage = new NavigationPage(new Page2());
    }

Problem :问题:

My problem is that the title and the BackGroundImage don't display.我的问题是标题和 BackGroundImage 不显示。 There are so many property that doesn't work.有很多属性不起作用。

My page is inherit form ContentPage but I can't access property of the ContentPage class.我的页面是继承表单 ContentPage 但我无法访问 ContentPage 类的属性。 Properties are public.属性是公开的。

Why is my title not being displayed?为什么我的标题不显示?

You can define the page as NavigationPage in App.xaml.cs, without declaring NavigationPage in the xaml file, ie like so in App.xaml.cs:您可以在 App.xaml.cs 中将页面定义为 NavigationPage,而无需在 xaml 文件中声明 NavigationPage,例如在 App.xaml.cs 中:

InitializeComponent();

MainPage = new NavigationPage(new Page2());

Try试试

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Rutul_App.Page2"
             Title="aaaa" NavigationPage.HasNavigationBar="True">

If above answers didn't worked then just be sure that you are keeping your MainActivity.cs and styles.xml with default values.如果上述答案不起作用,那么请确保您将 MainActivity.cs 和 styles.xml 保留为默认值。 eg例如

[Activity(Label = "SMSMobile.Droid", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = false, NoHistory = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;
        base.OnCreate(bundle);
        global::Xamarin.Forms.Forms.Init(this, bundle);
        LoadApplication(new App());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM