2012-09-06 43 views
0

我嘗試在WPF中的RichTextBox中添加表情符號,使用下面的函數時,當我從文本框鍵入文本:)它顯示良好的結果,但是當我再次鍵入其他文本顯示它錯誤 (使用相同的密鑰已經被添加的項目)。這是我從下面函數調用表情代碼:當添加表情到RichTextBox錯誤(**具有相同的鍵的項目已被添加**)

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);  
    } 

I型修復它但仍然沒有解決。我希望所有的程序員都會幫助我。 感謝

///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); 

      } 

回答

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"); 
+0

謝謝,但我想添加許多表情符號,我該怎麼辦? – ppp

+0

@ppp,將上述提到的行放入您的'Window_Loaded'事件 – Habib

+0

非常感謝您,它爲我做得很好。 – ppp

相關問題