在Xamarin.Forms

时间:2016-05-23 03:46:32

标签: xamarin xamarin.android xamarin.forms xamarin-studio

我在设置应用程序的根页面时遇到问题。我无法显示正确的页面。第一个Mainpage是从头开始的默认页面,第二个MainPage是我创建的页面的名称。这是我的代码。

App.cs

   public App()
    {   
        MainPage = new NavigationPage(new MainPage());

    }

这是我想要设置为根页面的页面代码。

MainPage.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"
         xmlns:local="clr-namespace:XamarinDemoApp"
         x:Class="XamarinDemoApp.MainPage"
         xmlns:ViewModels="clr-namespace:XamarinDemoApp.ViewModels;assembly=XamarinDemoApp">

    <ContentPage.BindingContext>
      <ViewModels:MainViewModel />
    </ContentPage.BindingContext>


   <ListView ItemsSource="{Binding EmployeesList}"
        HasUnevenRows="True">

<ListView.ItemTemplate>
  <DataTemplate>
    <ViewCell>
      <StackLayout Orientation="Horizontal">
        <Label Text="{Binding Name}"
               FontSize="24"/>
        <Label Text="{Binding Department}"
               FontSize="18"
               Opacity="0.6"/>
      </StackLayout>
    </ViewCell>
  </DataTemplate>
</ListView.ItemTemplate>


</ListView>





</ContentPage>

这是我的MainPage.xaml.cs背后的代码

   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;
   using System.Threading.Tasks;

   using Xamarin.Forms;
   using XamarinDemoApp.ViewModels;

   namespace XamarinDemoApp
   {
       public partial class MainPage : ContentPage
       {
           public MainPage()
           {
        InitializeComponent();
        NavigationPage.SetHasNavigationBar(this, false);

           }
       }
   }

这是我得到的Stack Trace字段。

    [External Code] 
>   0xC in XamarinDemoApp.MainPageMain.InitializeComponent at c:\Users\LIFEBOOK E743\Documents\Visual Studio 2013\Projects\XamarinDemoApp\XamarinDemoApp\obj\Debug\XamarinDemoApp.Views.MainPageMain.xaml.g.cs:21,13    C#
    0x9 in XamarinDemoApp.MainPageMain..ctor at c:\Users\LIFEBOOK E743\Documents\Visual Studio 2013\Projects\XamarinDemoApp\XamarinDemoApp\Views\MainPageMain.xaml.cs:16,13 C#
    0x9 in XamarinDemoApp.App..ctor at c:\Users\LIFEBOOK E743\Documents\Visual Studio 2013\Projects\XamarinDemoApp\XamarinDemoApp\App.cs:14,12  C#
    0x12 in XamarinDemoApp.Droid.MainActivity.OnCreate at c:\Users\LIFEBOOK E743\Documents\Visual Studio 2013\Projects\XamarinDemoApp\XamarinDemoApp.Droid\MainActivity.cs:20,13    C#
    0x13 in Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ at /Users/builder/data/lanes/3053/a94a03b5/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.App.Activity.cs:2857,4  C#

1 个答案:

答案 0 :(得分:0)

这是在错误的地方......

NavigationPage.SetHasNavigationBar(this, false);

应该是

public App()
{   
    var mainPage = new NavigationPage(new MainPage());
    NavigationPage.SetHasNavigationBar(this, false);
    MainPage = mainPage;

}