0

我尝试在 WPF 的 RichTextBox 中添加使用下面功能的表情符号,当我从 Textbox 键入文本 :) 时,它显示了良好的结果,但是当我再次键入其他文本时它显示错误(已添加具有相同键的项目).这是我从下面的 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);

            }
4

1 回答 1

3

您收到此错误的原因是因为您在字典中添加了一个具有相同键的对象,实际上您_mappings在每次调用函数时添加了字典中的所有项目Emoticons。您不能在字典中添加重复条目。我不知道您为什么要这样做,但是您可以在应用程序启动或窗口加载事件中填写一次字典,然后再使用它。

将以下行移至仅执行一次的任何方法/事件。

_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");
于 2012-09-06T04:53:57.323 回答