-1

我正在调用具有Grid.Children.Clear()功能的方法。从不同的方法调用它时效果很好。但是当我从 xmpp_onmessage 方法调用我的方法时。我遇到错误。“调用线程无法访问此对象,因为不同的线程拥有它。”</p>

这是包含 Grid.Children.Clear() 的方法:

 private void ConstructChatView(Boolean isChat)
    {   
        System.Uri resourceUri = new System.Uri("Public/Images/chat_green-textarea.png", UriKind.Relative);
        StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri);

        System.Uri resourceUri2 = new System.Uri("Public/Images/chat_green-textarea-tail.png", UriKind.Relative);
        StreamResourceInfo streamInfo2 = Application.GetResourceStream(resourceUri2);

        System.Uri resourceUri3 = new System.Uri("Public/Images/chat_blue-textarea.png", UriKind.Relative);
        StreamResourceInfo streamInfo3 = Application.GetResourceStream(resourceUri3);

        System.Uri resourceUri4 = new System.Uri("Public/Images/chat_blue-textarea-tail.png", UriKind.Relative);
        StreamResourceInfo streamInfo4 = Application.GetResourceStream(resourceUri4);


        BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream);
        var brush = new ImageBrush();
        brush.ImageSource = temp;

        BitmapFrame temp2 = BitmapFrame.Create(streamInfo2.Stream);

        BitmapFrame temp3 = BitmapFrame.Create(streamInfo3.Stream);
        var brush2 = new ImageBrush();
        brush2.ImageSource = temp3;

        BitmapFrame temp4 = BitmapFrame.Create(streamInfo4.Stream);

        int ctr = 0;
        chatGrid.Children.Clear();
        if (isChat == true)
        {
            for (int i = 0; i < _messageView.Count; i++)
            {

                if ((!_messageView.ElementAt(i).Message.ToString().Trim().Equals("")))
                {
                    RowDefinition chatGridRow1 = new RowDefinition();
                    RowDefinition chatGridRow2 = new RowDefinition();
                    RowDefinition chatGridRow3 = new RowDefinition();

                    chatGrid.RowDefinitions.Add(chatGridRow1);
                    chatGrid.RowDefinitions.Add(chatGridRow2);
                    chatGrid.RowDefinitions.Add(chatGridRow3);

                    if (_messageView.ElementAt(i).IsMe == true)
                    {
                        TextBlock Message = new TextBlock();
                        Message.Foreground = Brushes.White;
                        Message.Padding = new Thickness(10, 10, 10, 10);
                        Message.HorizontalAlignment = HorizontalAlignment.Right;
                        Message.Margin = new Thickness(0, 0, 5, 0);
                        Message.Background = brush2;
                        Message.TextWrapping = TextWrapping.Wrap;
                        Message.Text = _messageView.ElementAt(i).Message;
                        Grid.SetRow(Message, ctr);
                        Grid.SetColumn(Message, 0);
                        ctr++;

                        Image Bluetail = new Image();
                        Bluetail.Source = temp4;
                        Bluetail.HorizontalAlignment = HorizontalAlignment.Right;
                        Bluetail.Height = 10;
                        Bluetail.Width = 20;
                        Bluetail.Margin = new Thickness(0, -(0.7), 10, 0);
                        Grid.SetRow(Bluetail, ctr);
                        ctr++;

                        Label Sender = new Label();
                        Sender.Foreground = Brushes.White;
                        Sender.Margin = new Thickness(0, 0, 0, 10);
                        Sender.HorizontalAlignment = HorizontalAlignment.Right;
                        Sender.Content = "Sent By : " + _messageView.ElementAt(i).Name.ToString() + " " + _messageView.ElementAt(i).DateCreated.ToString();
                        Grid.SetRow(Sender, ctr);
                        Grid.SetColumn(Sender, 0);
                        ctr++;

                        chatGrid.Children.Add(Message);
                        chatGrid.Children.Add(Bluetail);
                        chatGrid.Children.Add(Sender);
                    }
                    else
                    {
                        TextBlock Message = new TextBlock();
                        Message.Foreground = Brushes.White;
                        Message.Padding = new Thickness(10, 10, 10, 10);
                        Message.HorizontalAlignment = HorizontalAlignment.Left;
                        Message.Margin = new Thickness(5, 0, 0, 0);
                        Message.Background = brush;
                        Message.TextWrapping = TextWrapping.Wrap;
                        Message.Text = _messageView.ElementAt(i).Message;
                        Grid.SetRow(Message, ctr);
                        Grid.SetColumn(Message, 0);
                        ctr++;

                        Image Greentail = new Image();
                        Greentail.Source = temp2;
                        Greentail.HorizontalAlignment = HorizontalAlignment.Left;
                        Greentail.Height = 10;
                        Greentail.Width = 20;
                        Greentail.Margin = new Thickness(10, -(0.7), 5, 0);
                        Grid.SetRow(Greentail, ctr);
                        ctr++;

                        Label Sender = new Label();
                        Sender.Foreground = Brushes.White;
                        Sender.Margin = new Thickness(0, 0, 0, 10);
                        Sender.HorizontalAlignment = HorizontalAlignment.Left;
                        Sender.Content = "Sent By : " + _messageView.ElementAt(i).Name.ToString() + " " + _messageView.ElementAt(i).DateCreated.ToString();
                        Grid.SetRow(Sender, ctr);
                        Grid.SetColumn(Sender, 0);
                        ctr++;

                        chatGrid.Children.Add(Message);
                        chatGrid.Children.Add(Greentail);
                        chatGrid.Children.Add(Sender);
                    }
                }

            }
        }
        else
        {
            for (int i = 0; i < _messageView.Count; i++)
            {

                if (_messageView.ElementAt(i).IsMe == true && (!_messageView.ElementAt(i).Message.ToString().Trim().Equals("")))
                {
                    RowDefinition chatGridRow1 = new RowDefinition();
                    RowDefinition chatGridRow2 = new RowDefinition();
                    RowDefinition chatGridRow3 = new RowDefinition();

                    chatGrid.RowDefinitions.Add(chatGridRow1);
                    chatGrid.RowDefinitions.Add(chatGridRow2);
                    chatGrid.RowDefinitions.Add(chatGridRow3);


                    TextBlock Message = new TextBlock();
                    Message.Foreground = Brushes.White;
                    Message.Margin = new Thickness(0, 10, 300, 0);
                    Message.Padding = new Thickness(10, 10, 10, 10);
                    Message.HorizontalAlignment = HorizontalAlignment.Left;
                    Message.Background = brush;
                    Message.TextWrapping = TextWrapping.Wrap;
                    Message.Text = _messageView.ElementAt(i).Message;
                    Grid.SetRow(Message, ctr);
                    Grid.SetColumn(Message, 0);
                    ctr++;

                    Image Greentail = new Image();
                    Greentail.Source = temp2;
                    Greentail.HorizontalAlignment = HorizontalAlignment.Left;
                    Greentail.Height = 10;
                    Greentail.Width = 20;
                    Greentail.Margin = new Thickness(5, -(0.7), 0, 0);
                    Grid.SetRow(Greentail, ctr);
                    ctr++;

                    Label Sender = new Label();
                    Sender.Foreground = Brushes.White;
                    Sender.Margin = new Thickness(0, 0, 0, 10);
                    Sender.Content = "Sent By : " + _messageView.ElementAt(i).Name.ToString() + " " + _messageView.ElementAt(i).DateCreated.ToString();
                    Grid.SetRow(Sender, ctr);
                    Grid.SetColumn(Sender, 0);
                    ctr++;

                    chatGrid.Children.Add(Message);
                    chatGrid.Children.Add(Greentail);
                    chatGrid.Children.Add(Sender);
                }

            }
        }
        //for (int i = 0; i < _messageView.Count; i++)
        //{
        //    if (_messageView.ElementAt(i).IsMe == true && (!_messageView.ElementAt(i).Message.ToString().Trim().Equals("")))
        //    {
        //    }


        //}
        ctr = 0;

        scrollView.ScrollToEnd();
    }

有任何想法吗?谢谢

4

1 回答 1

1

大多数 UI 元素只能在 UI 线程中修改。由于您的事件处理程序显然是在不同的线程中调用的,因此您必须使用Dispatcher来调用 UI 线程中的代码。

private void ConstructChatView(Boolean isChat)
{
    Dispatcher.Invoke((Action)(() => chatGrid.Children.Clear()));
}

编辑:您还可以使用匿名方法将更多代码传递给 Invoke 调用:

private void ConstructChatView(Boolean isChat)
{
    Dispatcher.Invoke((Action)(() =>
        {
            // more code here
        }));
}

当然,您也可以将一堆代码放在另一个方法中并将其传递给 Invoke 调用:

private void ConstructChatView(Boolean isChat)
{
    Dispatcher.Invoke((Action)(() => ConstructChatViewInUI(isChat)));
}

private void ConstructChatViewInUI(Boolean isChat)
{
    ...
}
于 2013-05-31T07:40:59.343 回答