I am writing a chat application in WPF (using MVVM), When the user submits a message into the chat stream, I want to replace any smiley expressions such as :-) :-/ :-D, etc by actual smiley icons.
I have written a converter to do a linear search on the message and identify a smiley. My question is once I have identified a smiley expression, how can I replace the block of text which contains the smiley with the actual icon?
If you feel there is a better or efficient way of doing this, I would love to know...
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// The message passed in to the converter by calling XAML code
var message = System.Convert.ToString(value);
// Perform a linear search on the message
for (int i = 0; i < message.Length - 1; i++)
{
var c = message[i];
// Look for the character ':'
if (c == ':'
// Ensure that it has 2 more characters after it
&& i + 2 <= message.Length - 1 &&
// If it's the last character then that's fine
((i + 2 == message.Length - 1) ||
// Or else it should be followed by an empty space
(i + 3 <= message.Length - 1 && message[i + 3] == ' ')))
{
var expression = message.Substring(i, 3);
message = message**.Replace(expression, @".\Emotions\1.png");**
}
}
return message;
}
The converter is called from my XAML
<TextBlock Text="{Binding Content, Converter={converter:EmotionConverter}}" />
This doesn't work, I think it just replaces the text, i instead need a way to pass back an image, any advise how I could do this?