c# - 将列表视图中的选定项目添加到另一个列表视图
问题描述
我想将每个已处理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);
}
解决方案
我终于找到了解决方法。现在我首先使用一个 List 来为我的对象数组提供包装器。
// ListView A (Source)
// when ItemSelected is processed this function is called
public void AddToHistory(Object obj)
{
string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "history.txt");
var _tempList = new List<Object>();
if (File.Exist(fileName)
{
var _tempContent = File.ReadAllText(fileName);
var json = JsonConvert.DeserializeObject<List<Object>>(tempContent);
_tempList.AddRange(json);
_tempList.Add(obj);
} else
{
_tempList.Add(obj);
}
var content = JsonConvert.SerializeObject(_tempList);
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<List<Object>>(content);
ObjectList.AddRange(json);
}
推荐阅读
- scrapy - scrapy 307 重定向到同一页面
- python - 将 alt.Chart() 与来自 google drive url 链接的数据一起使用
- java - 通过 SSL 连接的精简 JRE11 需要什么?
- javascript - Node 和使用节点模块的项目之间有什么联系?
- comsol - Comsol 电阻加热线圈模拟
- javascript - 如何减慢javascript中的循环
- javascript - 如果值有逗号,则循环并将对象字符串值转换为对象
- r - KNN 在 `class` 中有效,但在 `caret` 中无效(关系太多)
- php - 为什么此查询不适用于指定的日期?
- excel - 双重错误处理 - 错误后继续而不返回