对编程来说是全新的,我试图在Windows窗体中创建类似漫画组织工具的东西,但是我陷入了使用反序列化的json字符串中的数据填充列表框的问题,目前它仅正确显示布尔值,所有其他值甚至为“ 0”。我有一个按钮可以做到这一点:
using (OpenFileDialog openFileDialog = new OpenFileDialog(){Filter = "Json Files|*.json", ValidateNames = true, Multiselect = false })
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
var fileStream = openFileDialog.OpenFile();
using (StreamReader sr = new StreamReader(fileStream))
{
string fileContent = sr.ReadToEnd();
ComicList comics = JsonConvert.DeserializeObject<ComicList>(fileContent);
Manga_listBox.DataSource = DisplayComic.FullList;
//ignore this little bit it's just so i can see what's happening
label1.Text = Convert.ToString(comics.Comics.Count);
label1.Text = Convert.ToString(DisplayComic.FullList);
}
}
}
而且我有以下类似的类:
public class Comic
{
private string Manga;
private int Chapters;
private bool isFinished;
private int LastReadCH;
public string Manga1 { get => Manga; set => Manga = value; }
public int Chapters1 { get => Chapters; set => Chapters = value; }
public bool IsFinished { get => isFinished; set => isFinished = value; }
public int LastReadCH1 { get => LastReadCH; set => LastReadCH = value; }
public Comic(Comic asd)
{
this.Manga = Manga1;
this.Chapters = Chapters1;
this.IsFinished = IsFinished;
this.LastReadCH = LastReadCH1;
}
public override string ToString()
{
return string.Format("{0}, {1}, {2}, {3}",
this.Manga, this.Chapters, this.IsFinished, this.LastReadCH);
}
}
和
public class ComicList
{
private List<Comic> comics;
public List<Comic> Comics { get => comics; set => comics = value; }
}
和
public class DisplayComic
{
static DisplayComic()
{
using (OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = "Json Files|*.json", ValidateNames = true, Multiselect = false })
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
var fileStream = openFileDialog.OpenFile();
using (StreamReader sr = new StreamReader(fileStream))
{
string fileContent = sr.ReadToEnd();
ComicList comics = JsonConvert.DeserializeObject<ComicList>(fileContent);
FullList = comics.Comics;
}
}
}
}
private static List<Comic> fullList;
public static List<Comic> FullList { get => fullList; set => fullList = value; }
public static List<Comic> GetComcis()
{
return FullList;
}
}
代码可能很混乱,然后又是完全全新的,并且一直在网上跟踪各种信息,也由于某种原因,打开的对话框窗口打开了两次,我不明白为什么。这是使用的json文件:
{
"Comics": [
{
"Manga": "MangaNumber1",
"Chapters": 85,
"isFinished": true,
"LastReadCH": 85
},
{
"Manga": "MangaNumber2",
"Chapters": 112,
"isFinished": true,
"LastReadCH": 112
},
{
"Manga": "MangaNumber3",
"Chapters": 117,
"isFinished": true,
"LastReadCH": 117
},
{
"Manga": "MangaNumber4",
"Chapters": 74,
"isFinished": true,
"LastReadCH": 74
}
]
}
我已经用我的“专业知识”尝试了几乎所有的人都可以通过更改各种变量名等等来确实对我们有所帮助。
尝试设置列表框的“ DisplayMember”属性,以便控件知道您要查看的类的哪个属性。像这样的东西...
Manga_listBox.DataSource = DisplayComic.FullList;
Manga_listBox.DisplayMember = "Manga";