我创建了一个Xamarin Forms应用程序,它将用户输入的数据存储在SQLite数据库中,并在列表中显示。到这个地方一切都很好,数据被正确地存储和检索。然后我按照教程添加了MasterDetailPage。应用程序成功构建,但在MainActivity.cs的LoadApplication(new App());方法中出现了System.InvalidCastException: Specified cast is not valid In mgmain JNI_OnLoad异常。经过良好的搜索,我无法确定问题出在哪里。
这是App.cs,其中MainMenuPage是一个MasterDetailPage。
这是App.cs,其中MainMenuPage是一个MasterDetailPage。
namespace MyListXamarinForms
{
public class App : Application
{
public App()
{
MainPage = new MainMenuPage();
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
}
MainMenuPage.xaml是包含MasterPage和Detail的主页面(即母版页)-如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyListXamarinForms.MainMenuPage"
xmlns:local="clr-namespace:MyListXamarinForms">
<MasterDetailPage.Master>
<local:MasterPage x:Name="master"/>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<NavigationPage>
<x:Arguments>
<local:DetailPage/>
</x:Arguments>
</NavigationPage>
</MasterDetailPage.Detail>
</MasterDetailPage>
MainMenuPage.xaml.cs`
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace MyListXamarinForms
{
public partial class MainMenuPage : MasterDetailPage
{
public MainMenuPage()
{
InitializeComponent();
master.ListViews.ItemSelected += OnItemSelected;
}
private void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var item = e.SelectedItem as MasterPageItem;
if(item!=null)
{
Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
master.ListViews.SelectedItem = null;
IsPresented = false;
}
}
}
}`
MasterPage.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="MyListXamarinForms.MasterPage"
Icon=""
Title="Menu"
Padding="0,50,0,0">
<ContentPage.Content >
<StackLayout VerticalOptions="FillAndExpand">
<ListView x:Name="MenuList" VerticalOptions="FillAndExpand" SeparatorVisibility="None">
<ListView.ItemTemplate>
<DataTemplate>
<Label x:Name="ItemNameLabel" Text="{Binding Title}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
MasterPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace MyListXamarinForms
{
public partial class MasterPage : ContentPage
{
public ListView ListViews { get { return MenuList; } }
public MasterPage()
{
InitializeComponent();
var masterPageItem = new List<MasterPageItem>();
//AddItem and HomePage are two ContentPages with content
masterPageItem.Add(new MasterPageItem {
Title = "Add Item",
TargetType = typeof(AddItem)
});
masterPageItem.Add(new MasterPageItem
{
Title = "View Item List",
TargetType = typeof(HomePage)
});
MenuList.ItemsSource = masterPageItem;
}
}
}
MasterPageItem.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyListXamarinForms
{
public class MasterPageItem
{
public string Title { get; set; }
public Type TargetType { get; set; }
}
}
MyListXamarinForms.Droid MainActivity.cs
using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace MyListXamarinForms.Droid
{
[Activity(Label = "MyListXamarinForms", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, 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()); //Exception thrown here
}
}
}
`