【问题标题】:System.InvalidCastException with ListView in Xamarin.FormsSystem.InvalidCastException 与 Xamarin.Forms 中的 ListView
【发布时间】:2019-02-17 16:57:02
【问题描述】:

我目前正在使用 Xamarin.Forms 为 IOS 和 Android 创建一个应用程序,该应用程序接受预订并将预订信息放入 SQLite 数据库,然后将填充到 ListView 中,以便管理员删除或接受预订。数据库工作正常,但在尝试使用预订的 ListView 查看管理页面时出现“指定的演员表无效”错误。代码如下:

我试图研究这个问题,我认为它与 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);


    }

获取预订方法:

 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

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

【问题讨论】:

  • 请贴出App.UserDatabase.GetReservation()的来源;
  • @Jason 我发布了使用 GetReservation 方法的更新。

标签: c# xaml listview xamarin.forms


【解决方案1】:

您的 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>

【讨论】:

  • 如果我发布了更多关于我的数据库的信息,你能帮我吗?现在我可以查看列表视图中的任何内容。
  • 发布一个新问题,但我怀疑这与我之前对您的数据库查询提出的建议有关
  • 好的,我确实发布了一个新问题!感谢大家的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-19
相关资源
最近更新 更多