List 中不需要的重写数据

发布于 2024-10-16 00:30:14 字数 2582 浏览 2 评论 0原文

我在列表集合中存储数据时遇到问题。如果我添加新数据,它会重写旧数据,并且列表中仍然只有一项。

这是主要方法,从这个方法中我调用方法 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千柳 2024-10-23 00:30:14

现在,如果我理解你的例子。

您将对象添加到 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文