Reputation: 121
namespace FBLALibraryApp2
{
public partial class GenrePage : ContentPage
{
public GenrePage()
{
InitializeComponent();
listView.ItemsSource = DataStorage.GenreList;
}
private void SearchBar_TextChanged(object sender, TextChangedEventArgs e)
{
}
private void listView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
listView.SelectedItem = null;
}
async private void listView_ItemTapped(object sender, ItemTappedEventArgs e)
{
if (e.Item is GenreGroup)
await Navigation.PushAsync(new GenreView((GenreGroup)e.Item));
}
}
}
Here is the code for a page that I have made in Xamarin. It is a simple page that displays a list of GenreGroup's. It has worked fine in the past, but recently, when I changed my code initializing the list, the application would run to a blank screen. So, I paused debugging and saw it was stuck at, initially, listView.ItemSource = DataStorage.GenreList;. Upon hitting continue, the application would throw a TypeInitializationException. I was worried that perhaps the most recent version of my code was not being built (this has happened to me before with Fast Deployment enabled), so I added a Debug.log("x"); before the troublesome line to ensure that the most recent code was being run. Upon doing this, the application began freezing on InitializeComponent() and throwing the same exception. I am stumped here as to what to do. Here is the code for where I declare the list:
public static class DataStorage
{
public static List<Book> AllBookList = new List<Book> { new Book { Title = "Harry Potter", Author = "J.K. Rowling", Summary="lorem ipsum dolorum sdjfa;dklfja;dkfj;akdfja;dfjkaf", Genre= new string[] { "Fantasy", "Adventure" }, pubYear="2017" }, new Book { Title = "The Hunger Games", Author = "Suzanne Collins", Summary = "lorem ipsum dolorum sdjfa;dklfja;dkfj;akdfja;dfjkaf", Genre = new string[] { "Sci-Fi" }, pubYear = "2017" }, new Book { Title = "Thief in the Night", Author = "Jack Nimble", Summary = "lorem ipsum dolorum sdjfa;dklfja;dkfj;akdfja;dfjkaf", Genre = new string[] { "Mystery"}, pubYear = "2017" }, new Book { Title = "Hardy Bros.", Author = "Johnny Heather", Summary = "lorem ipsum dolorum sdjfa;dklfja;dkfj;akdfja;dfjkaf", Genre = new string[] { "Mystery", "Comedy" }, pubYear = "2017" } };
public static List<GenreGroup> GenreList = new List<GenreGroup> { new GenreGroup("Mystery", "jd;akfjd;"), new GenreGroup("Fantasy", "dja;fdjkl") };
public static GenreGroup Mystery = new GenreGroup("Mystery","djfsldfjldjF");
public static GenreGroup Fantasy = new GenreGroup("Fantasy", "djfsldfjldjF");
static DataStorage()
{
foreach (Book book in AllBookList)
{
for (int x = 0; x < book.Genre.Length; x++)
{
if (book.Genre[x] == "Mystery")
{
Mystery.Add(book);
}
else if (book.Genre[x] == "Fantasy")
{
Fantasy.Add(book);
}
}
}
GenreList = new List<GenreGroup> { Mystery, Fantasy };
}
}
I suspect the issue is somewhere in this code, as this is what I changed. Note: I understand that my code right now is a little strange and I define GenreList explicitly, and then redefine it later, but I was trying to ensure it wasn't null for whatever reason. Thanks for any help
Update: upon placing Debug.WriteLine BEFORE InitializeComponent, it now freezes on Debug.WriteLIne and throws the same, TypeInitializationEXception
Update: Here is the code for GenreGroup and Book
public class GenreGroup : List<Book>
{
public string Title { get; set; }
public string Description { get; set; }
public GenreGroup(string title, string description)
{
Title = title;
Description = description;
}
}
public class Book
{
public string Title { get; set; }
public string Author { get; set; }
public string Summary { get; set; }
public string[] subjectHeadings { get; set; }
public string[] Genre { get; set; }
public string pubYear { get; set; }
}
Upvotes: 0
Views: 506
Reputation: 1809
You' re missing initializations here:
public static GenreGroup Mystery; // Init this
public static GenreGroup Fantasy; // And this
Edit: I just ran your code and it's fine. Just clean your project and rebuild it. VS has plenty of bugs. Cheers.
Upvotes: 1
Reputation: 4358
listView.ItemsSource = DataStorage.GenreList;
This line will call static DataStorage()
, in this static constructor, Mystery
and Fantasy
should be initialized before you call it. So the solution should be this:
1) Add public GenreGroup() { }
in your GenreGroup
class;
2) Init Mystery
and Fantasy
in DataStorage
class:
public static GenreGroup Mystery = new GenreGroup();
public static GenreGroup Fantasy = new GenreGroup();
Upvotes: 0