[英]WPF C# Insert image inside textblock in a specific place (code)
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) 播放器1:您好*(图片)
Player 2: I do not want to talk with you *2(Image2) 播放器2:我不想与您交谈* 2(Image2)
But the my chat looks like this: 但是我的聊天看起来像这样:
Player 1: Hi * 玩家1:嗨*
Player: I do not want to talk with you *2 (Image)(Image2) 播放器:我不想和您聊天* 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. 图像的位置由特定字符表示(例如* o * 2)。我想使用普通的TextBlock而不是RichTextBlock。我认为使用普通的TextBlock可以做到这一点,因为它可以在xaml中完成。 Thanks for your attention and hopefully you can help me.
感谢您的关注,希望您能为我提供帮助。
"MiChatBox.Inlines.Add" It adds at the end of all. “ MiChatBox.Inlines.Add”它添加在所有末尾。 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.