List 中不需要的重写数据
我在列表集合中存储数据时遇到问题。如果我添加新数据,它会重写旧数据,并且列表中仍然只有一项。
这是主要方法,从这个方法中我调用方法 OpenChatScreen,它是 ChatScreenManager 类的方法,这是问题的根源。
private void OpenTabChatWindow(string nick)
{
try
{
new System.Threading.Tasks.Task(() =>
{
IDetailData oponent = new DetailData();
oponent = Service.DetailData(Account, nick);
Execute.OnUIThread((System.Action)(() =>
{
//here I call problem method OpenChatScreen method where is the problem,
//it use still the same reference on object opponent
if (ChatScreenManager.OpenChatScreen(true, Account, oponent, Account.DetailData.Info.Nick))
{
AddConversationHistory(nick);
}
}));
}
).Start();
}
catch (Exception exception)
{
MsgBox.ShowException(exception);
}
}
ChatScreenManager 类中的代码:
public IDictionary<string,object> ActiveChatScreens { get; set; }
or
public IList<string,> ActiveChatScreens { get; set; }
如果我使用字典或列表,问题是相同的。
public bool OpenChatScreen(bool useTabChat, IAccount account, IDetailData oponent, string avatarNick)
{
if (!ActiveChatScreens.Contains(oponent.Info.Nick))
{
if(useTabChat)
{
//in this method - OpenTabChat is problem
OpenTabChat(account, oponent, avatarNick);
return true;
}
}
return false;
}
private void OpenTabChat(IAccount account, IDetailData oponent, string avatarNick)
{
if (!ChatShellViewModel.IsActive)
{
OpenChatShell();
}
ChatShellViewModel.OpenChatTab(account, oponent, avatarNick);
//here is the root of problem, it use same reference of object opponent
ActiveChatScreens.Add(oponent.Info.Nick);
}
因此,我从 DetailData 的方法 OpenTabChatWindow 对象类型传递并将一些字符串属性存储在另一个类的 List 中,但在此对象上使用相同的引用并重写列表中的数据。
我尝试创建新的对象实例:
IDetailData oponent = new DetailData();
oponent = Service.DetailData(Account, nick);
并将该对象传递给问题方法,但它没有解决它。
I have problem with store data in list collection. If I add new data it rewrite old data, and in list is still only one item.
Here is main method, from this method I call method OpenChatScreen, It’s method of ChatScreenManager class where is root of problen.
private void OpenTabChatWindow(string nick)
{
try
{
new System.Threading.Tasks.Task(() =>
{
IDetailData oponent = new DetailData();
oponent = Service.DetailData(Account, nick);
Execute.OnUIThread((System.Action)(() =>
{
//here I call problem method OpenChatScreen method where is the problem,
//it use still the same reference on object opponent
if (ChatScreenManager.OpenChatScreen(true, Account, oponent, Account.DetailData.Info.Nick))
{
AddConversationHistory(nick);
}
}));
}
).Start();
}
catch (Exception exception)
{
MsgBox.ShowException(exception);
}
}
Code from ChatScreenManager class:
public IDictionary<string,object> ActiveChatScreens { get; set; }
or
public IList<string,> ActiveChatScreens { get; set; }
Problem is same if I use dictionary or list.
public bool OpenChatScreen(bool useTabChat, IAccount account, IDetailData oponent, string avatarNick)
{
if (!ActiveChatScreens.Contains(oponent.Info.Nick))
{
if(useTabChat)
{
//in this method - OpenTabChat is problem
OpenTabChat(account, oponent, avatarNick);
return true;
}
}
return false;
}
private void OpenTabChat(IAccount account, IDetailData oponent, string avatarNick)
{
if (!ChatShellViewModel.IsActive)
{
OpenChatShell();
}
ChatShellViewModel.OpenChatTab(account, oponent, avatarNick);
//here is the root of problem, it use same reference of object opponent
ActiveChatScreens.Add(oponent.Info.Nick);
}
So I pass from method OpenTabChatWindow object type of DetailData and store som string property in List in another class, but is use same reference on this object and rewrite data in list.
I try create new insatce of object:
IDetailData oponent = new DetailData();
oponent = Service.DetailData(Account, nick);
And pass this object to problem method, but it didn’t solve it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在,如果我理解你的例子。
您将对象添加到 ActiveChatScreens。
您是否首先在对 OpenChatScreen 的调用中重用了相同的对象。
那么对手会改变,并且在调用这些方法之前对它的任何引用也会改变吗?
请记住,对象是引用结构,即使更改其中的数据,引用仍然是相同的。
Now if I understand your examples.
You add objects to ActiveChatScreens.
Could it be that you are reusing the same object in the call to OpenChatScreen in the first place.
Id so then opponent will change and any reference to it will also change before these methods is even called?
Remember objects are reference structures, even if you change the data in them, the reference is still the same.