C# 添加MasterDetailPage后Xamarin.forms应用程序中的System.InvalidCastException
我创建了一个Xamarin表单应用程序,它将用户输入的数据存储在sqlite数据库中,然后显示在列表中。到目前为止,一切都很顺利。数据被正确地存储和检索。然后,我在教程之后添加MasterDetailPage。应用程序成功构建,但给出了System.InvalidCastException:指定的强制转换在mgmain JNI_OnLoad中无效。MainActivitiy.csLoadApplication(新应用程序())中出现异常方法。经过仔细搜索,我想不出问题出在哪里 这是App.cs,其中MainMenuPage是一个MasterDetailPageC# 添加MasterDetailPage后Xamarin.forms应用程序中的System.InvalidCastException,c#,xamarin,visual-studio-2015,xamarin.forms,C#,Xamarin,Visual Studio 2015,Xamarin.forms,我创建了一个Xamarin表单应用程序,它将用户输入的数据存储在sqlite数据库中,然后显示在列表中。到目前为止,一切都很顺利。数据被正确地存储和检索。然后,我在教程之后添加MasterDetailPage。应用程序成功构建,但给出了System.InvalidCastException:指定的强制转换在mgmain JNI_OnLoad中无效。MainActivitiy.csLoadApplication(新应用程序())中出现异常方法。经过仔细搜索,我想不出问题出在哪里 这是App.cs,
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.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>
机器人
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”时,您只在其内部放置了一个“标签”,因此出现了“无效强制转换”异常
<?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以外的东西,我很乐意知道
干杯:)在您的情况下,您不需要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中。在我的例子中,我使用CollectionView而不是ListView。它有一个自定义数据模板和一个EmptyView,这非常有帮助。 简介:
我排除了这个例外,从那以后就没有问题了。这是您在母版详细信息页面中所做的事情。没有代码,就不可能say@YuriS我已经用我的代码更新了帖子。任何帮助都将不胜感激。谢谢这会发生在应用程序发布时吗?你有没有定制的渲染器?另外,请仔细检查构建输出,有时其中包含一些有用的信息,尽管您需要仔细研究一下。为了简化,您可以公开共享此示例项目吗?例如,您缺少AddItem和HomePage的代码。问题也可能存在
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
}
}
}
<?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>
<TextCell x:Name="ItemNameLabel" Text="{Binding Title}"/>
</DataTemplate>
<DataTemplate>
<ViewCell>
<Label x:Name="ItemNameLabel" Text="{Binding Title}"/>
</ViewCell>
</DataTemplate>
<CollectionView x:Name="MenuList">
<CollectionView.ItemTemplate>
<DataTemplate>
<...Your cell here...>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>