Pregunta

I want to add an image inside a Textblock in a specific place during execution. I am doing a chat for a game and these images will be emoticons. I want to make a method which puts the image at the end of the sentence, but the one that I have made does not work well because the emoticon always appears at the end of the sentence.

The chat should look like this:

Player 1: Hi *(Image)

Player 2: I do not want to talk with you *2(Image2)

But the my chat looks like this:

Player 1: Hi *

Player: I do not want to talk with you *2 (Image)(Image2)

The code:

        foreach(char CharOfTheEmoticon in MiChatBox.Text)
        {

            if (CharOfTheEmoticon.Equals('*'))
            {
            BitmapImage MyImageSource = new BitmapImage(new Uri(@"..\..\..\..\Tarea6\Tarea3Frontend\NewImages\Smile.png", UriKind.Relative));
            Image image = new Image();
            image.Source = MyImageSource;
            image.Width = 15;
            image.Height = 15;
            image.Visibility = Visibility.Visible;
            InlineUIContainer container = new InlineUIContainer(image);
            Run run = new Run();
            run.Text = "*";
            MiChatBox.Inlines.Add(container);
            MiChatBox.Inlines.Add(run);
            }

         //More if for a differents images
        }

The position of the image is indicated by a specific char (for example * o *2) I want to use a normal TextBlock not a RichTextBlock I think that this is possible with a normal TextBlock because it can be done in the xaml. Thanks for your attention and hopefully you can help me.

¿Fue útil?

Solución

"MiChatBox.Inlines.Add" It adds at the end of all. That way you should do:

 var strBuild = new StringBuilder();
 var input = MiChatBox.Text;
 MiChatBox.Text = "";

 foreach (char CharOfTheEmoticon in input)
 {
     strBuild.Append(CharOfTheEmoticon);

     if (CharOfTheEmoticon == '*')
     {

         BitmapImage MyImageSource = new BitmapImage(new Uri(@"..\..\..\..\Tarea6\Tarea3Frontend\NewImages\Smile.png", UriKind.Relative));
         Image image = new Image();

         image.Source = MyImageSource;
         image.Width = 15;
         image.Height = 15;
         image.Visibility = Visibility.Visible;
         InlineUIContainer container = new InlineUIContainer(image);

         var originLastrText = new Run(strBuild.ToString());
         MiChatBox.Inlines.Add(originLastrText);
         MiChatBox.Inlines.Add(container);

         strBuild.Clear();
     }
 }

 var textRem = new Run(strBuild.ToString());
 MiChatBox.Inlines.Add(textRem);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top