我正在尝试开发一个功能,用户可以从hastags和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;
}
}
}
但是当我更改richeditbox的选择文本时,我会随机获取AccessViolation。它随机但频繁,以至于每次只能尝试复制它。我甚至可以抓住这个例外。我该怎么办?
与msdn here上的问题相似,根据该问题,它是一个已知问题。但Rudy huyn的6tag完美无瑕。如何?
我尝试使用TextBox更改RichEditBox,它仍然会在该控件上发生。
答案 0 :(得分:0)
通过更改RichEditBox外部的所有文本然后再添加它来解决它。这只是一个解决方法。我希望win rt更稳定,功能丰富,如银光。
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;
}
}
}
答案 1 :(得分:0)
您可以使用以下语法重试:
PostRichEditBox.Document.Selection.FormattedText.SetText(…);
这应该永久解决您的问题而不使用变通办法。 我的祝福!