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));
}
}
}
以下是我在Xamarin中制作的页面的代码。这是一个显示GenreGroup列表的简单页面。它在过去运行良好,但最近,当我更改我的代码初始化列表时,应用程序将运行到一个空白屏幕。所以,我暂停了调试,看到它最终被卡在了,listView.ItemSource = DataStorage.GenreList;。在按下继续时,应用程序将抛出TypeInitializationException。我担心也许我的代码的最新版本没有被构建(这在我之前启用了快速部署时发生了),所以我添加了一个Debug.log(" x");在麻烦的行之前确保最新的代码正在运行。执行此操作后,应用程序开始冻结InitializeComponent()并抛出相同的异常。我在这里难以理解该做什么。这是我声明列表的代码:
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 };
}
}
我怀疑问题出现在这段代码中,因为这是我改变的。注意:我理解我的代码现在有点奇怪,我明确地定义了GenreList,然后再重新定义它,但我试图确保它不会因为某种原因而无效。谢谢你的帮助
更新:在初始化组件之前放置Debug.WriteLine时,它现在在Debug.WriteLIne上冻结并抛出相同的TypeInitializationEXception
更新:这是GenreGroup和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; }
}
答案 0 :(得分:1)
您在这里缺少初始化:
public static GenreGroup Mystery; // Init this
public static GenreGroup Fantasy; // And this
编辑:我刚刚运行了您的代码,没关系。只需清理您的项目并重建它。 VS有很多错误。欢呼声。
答案 1 :(得分:0)
listView.ItemsSource = DataStorage.GenreList;
此行将调用static DataStorage()
,在此静态构造函数中,Mystery
和Fantasy
应在调用之前初始化。所以解决方案应该是这样的:
1)在public GenreGroup() { }
课程中添加GenreGroup
;
2)Mystery
类中的初始Fantasy
和DataStorage
:
public static GenreGroup Mystery = new GenreGroup();
public static GenreGroup Fantasy = new GenreGroup();