繁体   English   中英

Xamarin.Forms中具有ListView的System.InvalidCastException

[英]System.InvalidCastException with ListView in Xamarin.Forms

我当前正在使用Xamarin.Forms为IOS和Android创建一个应用程序,该应用程序进行保留并将保留信息放入SQLite数据库,然后该数据库将填充到ListView中供管理员删除或接受保留。 数据库工作正常,但是尝试使用保留ListView查看管理页面时出现“ Specified cast is invalid”错误。 代码如下:

我已经尝试研究此问题,并且认为它与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;


        }
    }
}

Reservation.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中的方法,它从日期选择器,时间选择器,滑块和下拉菜单中添加保留。

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);


    }

GetReservation方法:

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

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

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

        }

当我运行程序时,出现以下错误“ System.InvalidCastException”,指定的转换无效。 以下是StackTrace:

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:\a\1\s\Xamarin.Forms.Core\ItemsView.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:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:387
  at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) [0x0003d] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:573
  at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM