![](/img/trans.png)
[英]System.InvalidCastException with ListView in Xamarin.Forms
[英]System.InvalidCastException in Xamarin.forms application after adding MasterDetailPage
我创建了一个 Xamarin 表单应用程序,它将用户输入的数据存储在 sqlite 数据库中,然后显示在列表中。 到目前为止,一切都运行良好。 数据被正确存储和检索。 然后我按照教程添加一个 MasterDetailPage。 应用程序构建成功,但它给出System.InvalidCastException: Specified cast is not valid In mgmain JNI_OnLoad 。 MainActivitiy.cs LoadApplication(new App()) 中的异常; 方法。 经过一番良好的搜索,我无法弄清楚问题出在哪里。
这是 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;
}
}
}
}`
母版页.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>
母版页.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
}
}
}
`
在您的情况下,您不需要 ImageCell,因为您只是显示一个标签,但 ListView 的 DataTemplate 必须是一个单元格,因此您可以这样做:
<DataTemplate>
<TextCell x:Name="ItemNameLabel" Text="{Binding Title}"/>
</DataTemplate>
- TextCell 是带有标签的简单单元格。
或者这个:
<DataTemplate>
<ViewCell>
<Label x:Name="ItemNameLabel" Text="{Binding Title}"/>
</ViewCell>
</DataTemplate>
- 注意这只是将标签包装在 ViewCell 中
我有完全相同的问题。 我正在尝试自定义我的母版页,问题是当 DataTemplate 期望接收“ImageCell”时,您只将“标签”放入其中,因此出现“无效转换”异常。
<?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>
<ImageCell Text="{Binding Title}" ImageSource="{Binding IconSource}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
用上面的代码替换 DataTemplate,它应该可以工作。 尽管如果有人知道如何自定义 DataTemplate 以放置 ImageCell 以外的其他内容,我会很高兴知道!
干杯:)
就我而言,我使用 CollectionView 而不是 ListView。 它有一个自定义的 DataTemplate 和一个 EmptyView,这有很大帮助。 介绍: https : //docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/introduction
<CollectionView x:Name="MenuList">
<CollectionView.ItemTemplate>
<DataTemplate>
<...Your cell here...>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
我摆脱了这个例外,从那以后就没有问题了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.