TabbedPage和Content页面未显示

时间:2017-05-04 19:17:11

标签: android xamarin xamarin.forms xamarin.droid

我对Xamarin和XML相当新,尽管我有C#的经验。我在xml中创建了一个标签页,但它根本没有显示 Main.axml

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:mypages="clr-namespace:MainActivity.Pages;assembly=MainActivity"
            x:Class="MainActivity.tabPages">
  <TabbedPage.Children>

    <mypages:Home />
    <mypages:AddLocation />

  </TabbedPage.Children>
</TabbedPage>

我有2个标签页子项(主页和添加位置)我希望主页成为默认页面,但即使是TabbedPage也不会显示,应用程序为空白。

Home.axml

<?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="MainActivity.ActualPage" Title="Home" BackgroundColor="Green">
  <ContentPage.Content>

    <Label Text="Hi there from Page 1" TextColor="White" Font="20"
        VerticalOptions="Center" HorizontalOptions="Center" />

  </ContentPage.Content>
</ContentPage>

MainActivity.cs:

using Android.App;
using Android.Widget;
using Android.OS;

namespace Xonify
{
    [Activity(Label = "App", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView (Resource.Layout.Main);
        }
    }
}

Project's Strucutre

谢谢,

马特

1 个答案:

答案 0 :(得分:0)

您的MainActivity.cs未设置为支持Xamarin Forms,它本机加载视图。将您的MainActivity.cs更改为:

使用Android.App; 使用Android.Widget; 使用Android.OS;

namespace Xonify
{
    [Activity(Label = "App", MainLauncher = true, Theme = "@style/MainTheme", Icon = "@drawable/icon")]
    public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            Forms.Init(this, bundle);
            LoadApplication(new Xonify.PCL.App()); // Change to point to your App.xaml.cs class
        }
    }
}

您还需要在资源&gt;中创建一个文件调用styles.xml。在Values文件夹中添加此主题。

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <style name="MainTheme" parent="Theme.AppCompat.Light.DarkActionBar">
     <!-- You can change options in here, or change the parent style -->
  </style>
</resources>

如果您想查看示例,可以查看https://github.com/exrin/ExrinSample/blob/master/Exrin-Sample/ExrinSample.Droid/MainActivity.cs