繁体   English   中英

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

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

我正在研究 Xamarin.Forms。

当我创建空白的 Xamarin.Forms 项目时,在一个解决方案中创建了四个项目,一个用于 iOS,一个用于 Android,一个用于 Windows,一个是便携式项目(通用项目)。

我使用以下代码添加了一个名为“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>

并在文件背后的代码中:

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

在 App.cs 页面中,我添加了:

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

问题:

我的问题是标题和 BackGroundImage 不显示。 有很多属性不起作用。

我的页面是继承表单 ContentPage 但我无法访问 ContentPage 类的属性。 属性是公开的。

为什么我的标题不显示?

您可以在 App.xaml.cs 中将页面定义为 NavigationPage,而无需在 xaml 文件中声明 NavigationPage,例如在 App.xaml.cs 中:

InitializeComponent();

MainPage = new NavigationPage(new Page2());

试试

<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">

如果上述答案不起作用,那么请确保您将 MainActivity.cs 和 styles.xml 保留为默认值。 例如

[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