以下の関数を使用するWPFのRichTextBoxに絵文字を追加しようとすると、Textboxからtext :)と入力すると良い結果が表示されますが、他のテキストをもう一度入力するとエラーが表示されます(同じキーのアイテムが既に追加されています) )。これは、以下の関数Emoticonsから呼び出すコードです。
private void btnSend_Click(object sender, RoutedEventArgs e)
{
Paragraph p = new Paragraph();
p.LineHeight = 1;
Run dd = new Run();
dd.Text= DateTime.Now.ToString("(HH:mm:ss)") + "Chat" + rtbMessage.Text;
rtbBody.Document.Blocks.Add(p);
Emoticons(dd.Text);
}
私はそれを修正するためにタイプしますが、それでも解決しません。すべてのプログラマーが私を助けてくれることを願っています。ありがとう
///Function Emoticon////
private Dictionary<string, string> _mappings = new Dictionary<string, string>();
private string GetEmoticonText(string text)
{
string match = string.Empty;
int lowestPosition = text.Length;
foreach (KeyValuePair<string, string> pair in _mappings)
{
if (text.Contains(pair.Key))
{
int newPosition = text.IndexOf(pair.Key);
if (newPosition < lowestPosition)
{
match = pair.Key;
lowestPosition = newPosition;
}
}
}
return match;
}
// And also function which add smiles in richtextbox, here is it:
private void Emoticons(string msg)
{
//try
//{
_mappings.Add(@":)", "/Images/smiles/silly.png");
_mappings.Add(@">:o", "/Images/smiles/angry.png");
_mappings.Add(@":'(", "/Images/smiles/cry.png");
_mappings.Add(@"B-)", "/Images/smiles/cool.png");
_mappings.Add(@":^0", "/Images/smiles/laught.png");
_mappings.Add(@":-[", "/Images/smiles/blush.png");
_mappings.Add(@":-)", "/Images/smiles/happy.png");
_mappings.Add(@"?:|", "/Images/smiles/confuse.png");
_mappings.Add(@":x", "/Images/smiles/love.png");
var para = new Paragraph { LineHeight=1};
//Paragraph para = new Paragraph();
var r = new Run(msg);
para.Inlines.Add(r);
string emoticonText = GetEmoticonText(r.Text);
//if paragraph does not contains smile only add plain text to richtextbox rtb2
if (string.IsNullOrEmpty(emoticonText))
{
rtbBody.Document.Blocks.Add(para);
}
else
{
while (!string.IsNullOrEmpty(emoticonText))
{
TextPointer tp = r.ContentStart;
// keep moving the cursor until we find the emoticon text
while (!tp.GetTextInRun(LogicalDirection.Forward).StartsWith(emoticonText))
tp = tp.GetNextInsertionPosition(LogicalDirection.Forward);
// select all of the emoticon text
var tr = new TextRange(tp, tp.GetPositionAtOffset(emoticonText.Length)) { Text = string.Empty };
//relative path to image smile file
string path = _mappings[emoticonText];
var image = new Image
{
Source =
new BitmapImage(new Uri(Environment.CurrentDirectory + path,
UriKind.RelativeOrAbsolute)),
Width=Height=25,
};
//insert smile
new InlineUIContainer(image, tp);
if (para != null)
{
var endRun = para.Inlines.LastInline as Run;
if (endRun == null)
{
break;
}
else
{
emoticonText = GetEmoticonText(endRun.Text);
}
}
}
rtbBody.Document.Blocks.Add(para);
}