C#Frameworkelement OnRender未调用

时间:2018-06-28 09:13:24

标签: c# wpf multithreading user-interface thread-safety

我正在尝试制作一个基于服务器客户端的纸牌游戏,该游戏从服务器获取命令。

在客户端,我使用FrameworkElements,但是Onrender仅被调用一次。(程序启动时)

ui.Hand将用卡片填充玩家的手(我已经测试过其作品) 但是之后,在ui.Render中invalidateVisual不会调用On Render。

        private void ReceiveResponse()
    {
        while (true)
        {
            try
            {
                var buffer = new byte[2048];
                int received = ClientSocket.Receive(buffer, SocketFlags.None);
                if (received == 0) return;
                var data = new byte[received];
                Array.Copy(buffer, data, received);
                string text = Encoding.ASCII.GetString(data);
                if (text.Contains("#"))
                {
                    string[] cards = text.Split('#');
                    for (int i = 1; i < cards.Length; i++)
                    {
                        cards[i] = "Cards\\" + cards[i] + ".jpg";
                        //Dispatcher.Invoke(() => ui.Hand.Enqueue(new Card(new Point(this.ActualHeight - 300, 50 * i-1), cards[i])));     
                        lock (lockobject)
                        {
                            ui.Hand.Add(new Card(new Point(this.ActualHeight - 300, 50 + 200 * i - 1), cards[i]));
                            ui.Render();
                        }
                    }
                }
                Dispatcher.Invoke(() =>
                {
                    txt_msgs.Text += text;
                });
                var asd =ui.Hand;
            }
            catch (Exception)
            {
                break;
            }

        }
    }

Game Ui类:

        protected override void OnRender(DrawingContext drawingContext)
    {
        foreach (var card in Hand)
            {
                //drawingContext.DrawGeometry(card.texture, new Pen(), card.G);
                drawingContext.DrawGeometry(Brushes.Red, new Pen(), new EllipseGeometry(new Point(40, 40), 40, 40));
            }
    }

    public void Render()
    {
        this.InvalidateVisual();
    }

0 个答案:

没有答案