dimanche 21 février 2021

Error: "System.InvalidCastException: 'Specified cast is not valid."

I'm trying to run the app but when I run it gives this error Exception Thrown: "System.InvalidCastException: 'Specified cast is not valid.'" in the if statement.

Here is the code:

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    using Newtonsoft.Json;
    using EMN.Models;
    using Xamarin.Forms;


    namespace EMN
    {
        public partial class MainPage : ContentPage {

            private ObservableCollection<TestModel> myrootobject;
            public MainPage() {
                InitializeComponent();
                BindingContext = this;

                string[] dailyArray = new string[7] { "Lunedì", "Martedì", "Mercoledì", "Giovedì", "Venerdì", "Sabato", "Domenica" };

                MyDay.Text = dailyArray[0];

                var assembly = typeof(MainPage).GetTypeInfo().Assembly;
                Stream stream = assembly.GetManifestResourceStream("EMN.Dieta.json");

                using (var reader = new System.IO.StreamReader(stream)) {
                    var json = reader.ReadToEnd();

                    List<TestModel> mylist = JsonConvert.DeserializeObject<List<TestModel>>(json);
                        myrootobject = new ObservableCollection<TestModel>(mylist);

                    for(int i = 0; i < mylist.Count; i++) {
                
                        if (mylist[i].Giorno == "Lunedì") {
                            MyListView.ItemsSource = (System.Collections.IEnumerable)mylist[i];
                        }
                    }

                }
        
            }
        }
    }

Please, could you help me to fix this problem? Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire