I have created a Xamarin forms application which stores user entered data in sqlite database and then shows in a list. Till this point everything was working perfectly fine. The data was being stored and retrieved properly. I then add a MasterDetailPage following a tutorial. The application builds successfully but it gives System.InvalidCastException: Specified cast is not valid In mgmain JNI_OnLoad. exception in MainActivitiy.cs LoadApplication(new App()); method. After a good search, I can't figure out where could the problem be.
This is App.cs where MainMenuPage is a 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 (which is the masterpage that includes MasterPage and Detail - given below)`
<?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
}
}
}
`
In your case you don't need an ImageCell since you're just showing a label, but a DataTemplate for a ListView must be a Cell, so you could either do this:
<DataTemplate>
<TextCell x:Name="ItemNameLabel" Text="{Binding Title}"/>
</DataTemplate>
- TextCell is simple a cell with a label.
Or this:
<DataTemplate>
<ViewCell>
<Label x:Name="ItemNameLabel" Text="{Binding Title}"/>
</ViewCell>
</DataTemplate>
- Note how this is just wrapping the label in a ViewCell
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With