添加MasterDetailPage后,在Xamarin.forms应用程序中出现System.InvalidCastException错误

3
我创建了一个Xamarin Forms应用程序,它将用户输入的数据存储在SQLite数据库中,并在列表中显示。到这个地方一切都很好,数据被正确地存储和检索。然后我按照教程添加了MasterDetailPage。应用程序成功构建,但在MainActivity.cs的LoadApplication(new App());方法中出现了System.InvalidCastException: Specified cast is not valid In mgmain JNI_OnLoad异常。经过良好的搜索,我无法确定问题出在哪里。
这是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
        }
    }
}

`


这是在您的主从页面中执行的操作。没有代码,无法确定。 - Yuri S
@YuriS 我已经更新了帖子并附上了我的代码。任何帮助都将不胜感激。谢谢! - user5434084
它是否发生在应用启动时?您有没有任何定制渲染器的可能性?此外,请仔细检查构建输出,有时其中会有一些有用的信息,尽管您需要花费一些时间来查找它们。 - Gerald Versluis
为了简化操作,你能否公开分享这个示例项目? - Yuri S
例如,您可能缺少AddItem和HomePage的代码。问题也可能出在那里。 - Yuri S
3个回答

7
在您的情况下,由于您只需要显示一个标签而不是图像,所以您不需要使用ImageCell。但是,ListView的DataTemplate必须是一个单元格(Cell),因此您可以这样做:
<DataTemplate> 
    <TextCell x:Name="ItemNameLabel" Text="{Binding Title}"/> 
</DataTemplate>

- TextCell是一个带标签的简单单元格。 - 或者这样:
 <DataTemplate> 
     <ViewCell>
        <Label x:Name="ItemNameLabel" Text="{Binding Title}"/>
    </ViewCell>
</DataTemplate>

- 注意,这只是将标签包裹在一个 ViewCell 中。

2

我遇到了完全相同的问题。我正在尝试自定义我的主页,但问题在于当你只在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,我会很高兴知道!祝好:)

0
在我的情况下,我使用CollectionView而不是ListView。它有一个自定义的DataTemplate和一个EmptyView,这对我帮助很大。 介绍:https://learn.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>    

我用这个方法解决了异常问题,之后就再也没有出现过任何问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接