改变richditbox选择文本会抛出AccessViolationException



我正在尝试开发一个功能,用户可以从列表中选择标签和FriendList,并将它们添加到RichEditBox中,因为他使用自动建议,有点像自动完成。下面是完成此操作的代码:

private void MentionSuggestionList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.AddedItems.Count > 0)
        {
            bool flag = false;
            int index = 0;
            for (int i = PostRichEditBox.Document.Selection.EndPosition - 1; i >= 0; i--)
            {
                if (Char.IsLetterOrDigit(_postText[i]) || _postText[i] == '_')
                {
                    continue;
                }
                else if (_postText[i] == '@' || _postText[i] == '#')
                {
                    flag = true;
                    index = i + 1;
                    break;
                }
                else
                {
                    break;
                }
            }
            if (flag)
            {
                PostRichEditBox.Document.Selection.StartPosition = index;
                //AccessViolationException here
                PostRichEditBox.Document.Selection.SetText(TextSetOptions.None, (e.AddedItems[0] as MentionUserModel).screenName + " "); 
                PostRichEditBox.Document.Selection.StartPosition = PostRichEditBox.Document.Selection.EndPosition;
            }
        }
    }

但是当我改变richditbox的选择文本时,我随机得到AccessViolation。这是随机的,但它的频率足够高,我可以复制它,每次只尝试几次。我甚至可以捕获这个异常。我该怎么办?

类似于我在msdn上的问题,根据它是一个已知的问题。但鲁迪·胡恩的6号标签完美地做到了这一点。如何?

我试着用TextBox来改变RichEditBox,它仍然发生在那个控件上。

通过更改richditbox外部的所有文本然后将其添加回来来解决此问题。这只是一个变通方法。我希望winrt能像silverlight一样稳定,功能丰富。

private void MentionSuggestionList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int endpos = PostRichEditBox.Document.Selection.EndPosition;
        int startpos = PostRichEditBox.Document.Selection.StartPosition;
        if (e.AddedItems.Count > 0)
        {
            bool flag = false;
            int index = 0;
            for (int i = PostRichEditBox.Document.Selection.EndPosition - 1; i >= 0; i--)
            {
                if (Char.IsLetterOrDigit(_postText[i]) || _postText[i] == '_')
                {
                    continue;
                }
                else if (_postText[i] == '@' || _postText[i] == '#')
                {
                    flag = true;
                    index = i + 1;
                    break;
                }
                else
                {
                    break;
                }
            }
            if (flag)
            {
                //PostRichEditBox.Document.Selection.StartPosition = index;
                //PostRichEditBox.Document.Selection.EndPosition = endpos;
                String post="";
                PostRichEditBox.Document.GetText(TextGetOptions.None, out post);
                string result = post.Substring(0, index);
                result = result + (e.AddedItems[0] as MentionUserModel).screenName + " ";
                result = result + post.Substring(endpos, (post.Length - 1) - endpos);
                int size = endpos - index;
                size = ((e.AddedItems[0] as MentionUserModel).screenName + " ").Length - size;
                PostRichEditBox.Document.SetText(TextSetOptions.None, result);
                //PostRichEditBox.Document.Selection.SetText(TextSetOptions.None, (e.AddedItems[0] as MentionUserModel).screenName + " ");
                PostRichEditBox.Document.Selection.StartPosition = PostRichEditBox.Document.Selection.EndPosition = size + endpos;
            }
        }
    }

您可以使用以下语法重试:

PostRichEditBox.Document.Selection.FormattedText. settext(…);

这将永久解决您的问题,而无需使用变通方法。祝福你!

相关内容

  • 没有找到相关文章

最新更新