System.InvalidCastException with ListView in Xamarin.Forms



我当前正在为iOS和android创建一个使用xamarin.forms进行预订的应用程序,并将预订信息放入SQLITE数据库中删除或接受预订。数据库工作正常,但是当尝试使用"预订"列表视图查看管理页面时,我会发现"指定的铸件是无效的"错误。代码如下:

我试图研究这个问题,我认为它与XAML代码和绑定上下文有关,但不确定该如何解决该问题。

mypage.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="Assignment6.MyPage">
    <ContentPage.Content>
        <StackLayout>
            <ListView x:Name = "ListviewItems" />
            <ListView.ItemTemplate>
                <DataTemplate>
                  <ViewCell>
                        <ViewCell.View>
                            <StackLayout>
                            <Label Text = "{Binding date}" />
                            <Label Text = "{Binding meetingLocation}" />
                                </StackLayout>
                      </ViewCell.View>
                        </ViewCell>   
                    </DataTemplate>
                </ListView.ItemTemplate>
            </StackLayout>
    </ContentPage.Content>
</ContentPage>

mypage.xaml.cs

namespace Assignment6
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MyPage : ContentPage
    {
        private ObservableCollection<Reservation> items = new ObservableCollection<Reservation>();
        public MyPage()
        {
            InitializeComponent();
            Init();
        }
        public void Init()
        {
            BindingContext = new Reservation();
            var enumerator = App.UserDatabase.GetReservation();
            while (enumerator.MoveNext())
            {
                items.Add(enumerator.Current);
            }
            ListviewItems.ItemsSource = items;

        }
    }
}

保留。cs

public class Reservation
    {
        [PrimaryKey][AutoIncrement]
        public int date { get; set; }
        public int startingTime { get; set; }
        public string duration { get; set; }
        public int meetingLocation { get; set; }

        public Reservation()
        {
        }
        public Reservation(int date, int startingTime, string duration, int meetingLocation)
        {
            this.date = date;
            this.startingTime = startingTime;
            this.duration = duration;
            this.meetingLocation = meetingLocation;

        }
    }

从mainpage.xaml.cs的方法添加了datepicker,Time Picker,滑块和下拉菜单的预订。

void Handle_Clicked(object sender, System.EventArgs e)
        {
            DisplayAlert("Reservation", "Your reservation for a " + Output.Text + " meeting in room " + Room.SelectedIndex +
                 " starting at " + Time.Time + " on " + Date.Date.Month + "/" + Date.Date.Day + "/" + " " + Date.Date.Year + " for " + Slide.Value.ToString() + " hours has been added.", "OK");
        Reservation reserve = new Reservation(Date.Date.Month, Time.Time.Hours, Slide.Value.ToString(), Room.SelectedIndex);

        App.UserDatabase.SaveReservation(reserve);

    }

getRerverative方法:

 public IEnumerator<Reservation> GetReservation()
        {
            lock (locker)
            {
                if (database.Table<Reservation>().Count() == 0)
                {
                    return null;
                }
                else
                {
                    return database.Table<Reservation>().GetEnumerator();
                }
            }
        }

主页按钮单击方法,该方法将用户带到管理页面,并带有列表视图:

 async void Handle_Clicked_1(object sender, System.EventArgs e)
        {
            await Navigation.PushAsync(new MyPage());
        }

运行程序时,我会收到以下错误" system.invalidcastException"指定的铸件无效。以下是堆栈:

System.InvalidCastException: Specified cast is not valid.
  at Xamarin.Forms.ItemsView`1+<>c[TVisual].<.cctor>b__24_0 (Xamarin.Forms.BindableObject b, System.Object v) [0x00000] in D:a1sXamarin.Forms.CoreItemsView.cs:25
  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00071] in D:a1sXamarin.Forms.CoreBindableObject.cs:387
  at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) [0x0003d] in D:a1sXamarin.Forms.CoreBindableObject.cs:573
  at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in D:a1sXamarin.Forms.CoreBindableObject.cs:99
  at Assignment6.MyPage.InitializeComponent () [0x00023] in /Users/zane/Projects/Assignment6/Assignment6/obj/Debug/netstandard2.0/MyPage.xaml.g.cs:26
  at Assignment6.MyPage..ctor () [0x00013] in /Users/zane/Projects/Assignment6/Assignment6/MyPage.xaml.cs:16
  at Assignment6.MainPage+<Handle_Clicked_1>d__2.MoveNext () [0x0000f] in /Users/zane/Projects/Assignment6/Assignment6/MainPage.xaml.cs:33
  at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.2.1.13/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1023
  at Foundation.NSAsyncSynchronizationContextDispatcher.Apply () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.2.1.13/src/Xamarin.iOS/Foundation/NSAction.cs:178
  at at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.2.1.13/src/Xamarin.iOS/UIKit/UIApplication.cs:79
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0002c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.2.1.13/src/Xamarin.iOS/UIKit/UIApplication.cs:63
  at Assignment6.iOS.Application.Main (System.String[] args) [0x00001] in /Users/zane/Projects/Assignment6/Assignment6.iOS/Main.cs:17

对此的任何帮助将不胜感激!我一直在尝试调试一段时间,但我似乎无法弄清楚问题是什么。

您的 ItemTemplate需要嵌套在您的ListView块中

      <ListView x:Name = "ListviewItems" >
        <ListView.ItemTemplate>
            <DataTemplate>
              <ViewCell>
                    <ViewCell.View>
                        <StackLayout>
                        <Label Text = "{Binding date}" />
                        <Label Text = "{Binding meetingLocation}" />
                            </StackLayout>
                  </ViewCell.View>
                    </ViewCell>   
                </DataTemplate>
            </ListView.ItemTemplate>
          </ListView>

最新更新