C#连载/ deserialising列表视图 [英] C# Serialising/deserialising Listview

查看:126
本文介绍了C#连载/ deserialising列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个ListView连载的数据以及随后deserialising它也因此与保存的数据填充ListView控件。

I'm trying to serialise data from a ListView as well as then deserialising it and also consequently fill the ListView with the saved data.

 public List<ListViewItem> SaveListView(ListView LV)
    {
        System.Collections.Generic.List<ListViewItem> lSavedLV = new List<ListViewItem>();

        for (int i = 0; (i <= animalList.Count - 1); i++)
        {
            lSavedLV.Add(LV.Items[i]);
        }

        return lSavedLV;
    }

是,对于节省一部分合理的解决方案?是要走的路,如果我打算以填补这种序列化数据在ListView?这是那么匹配加载方法:

Is that a plausible solution for the saving part? Is that the way to go, if I plan to fill the ListView with such serialised data? Is this then a matching loading method:

 public ListView LoadListView(List<ListViewItem> L)
        {
            ListView lv = new ListView();

            for (int i = 0; (i <= (L.Count - 1)); i++)
            {
                ListViewItem lvi = new ListViewItem();
                lvi = ((ListViewItem)(L[i]));
                lv.Items.Add(lvi);
            }

            return lv;
        }

我的心真的飘荡,我一直在尝试一些不同的东西,但它似乎有一些错误的ListView控件的加载/灌装。它的详细信息视图。创意,技巧?

My mind really wanders, I've been trying a few different things but it seems that the there is something wrong with the loading/filling of the ListView. It's detail view. Ideas, tips?

推荐答案

不是想在这里光顾,但一个ListView是presentation逻辑。一个ListViewItem的是presentation逻辑。你不应该序列化的。你应该做的是序列化数据,而不是它的视觉再presentation。

Not trying to be patronizing here, but a ListView is presentation logic. A ListViewItem is presentation logic. You should not serialize those. What you should do is serialize your data, not its visual representation.

为什么不包含要在可视化数据的类?

Why not have a class which contains the data to be visualized?

public class DataIWannaVisualize
{
    // ...
}

请可序列化给它适当的属性:

Make it serializable by giving it the appropriate attribute:

[Serializable]
public class DataIWannaVisualize

和拥有这些数据对象的列表。

And have a List of these data objects.

IList<DataIWannaVisualize> dataList = new List<DataIWannaVisualize>();

这个列表包含了你将要做出可见,上你做你的变化,等数据。并且,当然,也可以很容易地被序列化和反序列化。

This list contains the data that you are about to make visible, on which you do your changes, and so on. And which, of course, can also easily be serialized or deserialized.

using (Stream stream = File.Open("data.bin", FileMode.Create))
{
    BinaryFormatter formatter = new BinaryFormatter();
    formatter.Serialize(stream, dataList);
}

现在,您的ListView。通过让你的窗体的Load事件的处理程序填充它。

Now, your ListView. Fill it by having a handler on the Load event of your Form.

private void Form1_Load(object sender, EventArgs e)
{
    foreach (DataIWannaVisualize dataObject in dataList)
    {
        ListViewItem item = new ListViewItem();
        // TODO: Fill the item with the desired data.
        listView1.Items.Add(item);
    }
}

和所有最好的,你会不会担心序列化UI逻辑类。

And best of all, you won't have to worry about serializing UI logic classes.

这篇关于C#连载/ deserialising列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆