将列表视图中的选定项目添加到另一个列表视图

我想将每个已处理SelectedItem的 from添加ListView A为ListView B一种历史记录。如果我只为一个对象编写此代码,但当我尝试向其中添加另一个对象时,ListView B它没有显示任何内容。我知道我必须将其反序列化为 aList<obj>但它不起作用。你能帮我吗?


这是我到目前为止所尝试的:


// ListView A (Source)

// the ItemSelected is processed this function is called


public void AddToHistory(Object obj)

{

    string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "history.txt");

    var content = JsonConvert.SerializeObject(obj);

    File.WriteAllText(fileName, content);

}


// ListView B (Destination View)

void CreateListOfObjects()

{

    ObjectList = new List<Object>();

    string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "history.txt");

    var content = File.ReadAllText(fileName);

    var json = JsonConvert.DeserializeObject<Object>(content);

    ObjectList.Add(json);

}


private List<Object> _object;

public List<Object> ObjectList

{

    get => _object;

    set => SetValue(ref _object, value);

}


当年话下
浏览 100回答 2
2回答

狐的传说

尝试使用 File.AppendAllText 而不是 File.WriteAllText,因为 WriteAllText 会在写入时覆盖现有文件。public void CreateListOfObjects()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "history.txt");&nbsp; &nbsp; &nbsp; &nbsp; var content = File.ReadAllText(fileName);&nbsp; &nbsp; &nbsp; &nbsp; var itemList = JsonConvert.DeserializeObject<List<string>>(content);&nbsp; &nbsp; &nbsp; &nbsp; foreach(var item in itemList)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listView2.Items.Add(item);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

慕婉清6462132

我终于找到了解决方法。现在我首先使用一个 List 来为我的对象数组提供包装器。// ListView A (Source)// when ItemSelected is processed this function is calledpublic void AddToHistory(Object obj){&nbsp; &nbsp; string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "history.txt");&nbsp; &nbsp; var _tempList = new List<Object>();&nbsp; &nbsp; if (File.Exist(fileName)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var _tempContent = File.ReadAllText(fileName);&nbsp; &nbsp; &nbsp; &nbsp; var json = JsonConvert.DeserializeObject<List<Object>>(tempContent);&nbsp; &nbsp; &nbsp; &nbsp; _tempList.AddRange(json);&nbsp; &nbsp; &nbsp; &nbsp; _tempList.Add(obj);&nbsp; &nbsp; } else&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _tempList.Add(obj);&nbsp; &nbsp; }&nbsp; &nbsp; var content = JsonConvert.SerializeObject(_tempList);&nbsp; &nbsp; File.WriteAllText(fileName, content);}// ListView B (Destination View)void CreateListOfObjects(){&nbsp; &nbsp; ObjectList = new List<Object>();&nbsp; &nbsp; string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "history.txt");&nbsp; &nbsp; var content = File.ReadAllText(fileName);&nbsp; &nbsp; var json = JsonConvert.DeserializeObject<List<Object>>(content);&nbsp; &nbsp; ObjectList.AddRange(json);}
打开App,查看更多内容
随时随地看视频慕课网APP