Ejemplo n.º 1
0
        /// <summary>
        /// Handles the Click event of the SendButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void SendButton_Click(object sender, RoutedEventArgs e)
        {
            string message = InputTextBox.Text;

            if (!string.IsNullOrEmpty(message))
            {
                // Create a new message info object and fill it with some data. This message info object is converted
                // to textblock controls and animated image using a converter.

                AnimationMessageInfo messageInfo = new AnimationMessageInfo();
                messageInfo.Created      = DateTime.Now;
                messageInfo.Message      = message;
                messageInfo.MessageBrush = new SolidColorBrush(Colors.Green);
                messageInfo.User         = "******";

                HistoryListBox.Items.Add(messageInfo);

                InputTextBox.Text = string.Empty;
            }
        }
        /// <summary>
        /// Modifies the source data before passing it to the target for display in the UI.
        /// </summary>
        /// <param name="value">The source data being passed to the target.</param>
        /// <param name="targetType">The <see cref="T:System.Type"/> of data expected by the target dependency property.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>
        /// The value to be passed to the target dependency property.
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            AnimationMessageInfo message = value as AnimationMessageInfo;

            if (message != null)
            {
                WrapPanel wrapPanel = new WrapPanel();
                wrapPanel.Children.Add(new TextBlock {
                    FontWeight = FontWeights.SemiBold, Text = "["
                });
                wrapPanel.Children.Add(new TextBlock {
                    FontWeight = FontWeights.SemiBold, Text = message.Created.ToString()
                });
                wrapPanel.Children.Add(new TextBlock {
                    FontWeight = FontWeights.SemiBold, Text = "]"
                });

                wrapPanel.Children.Add(new TextBlock {
                    Margin = new Thickness(4, 0, 0, 0), Text = message.User
                });
                wrapPanel.Children.Add(new TextBlock {
                    Margin = new Thickness(4, 0, 4, 0), Text = ":"
                });

                string messageString = message.Message;

                while (true)
                {
                    int    index     = int.MaxValue;
                    string smileyKey = null;

                    foreach (string key in _smileys.Keys)
                    {
                        int temp = messageString.IndexOf(key);

                        if (temp >= 0 && temp < index)
                        {
                            index = temp;

                            smileyKey = key;
                        }
                    }

                    if (index != int.MaxValue)
                    {
                        string subString = messageString.Substring(0, index);

                        foreach (string part in subString.Split(' '))
                        {
                            wrapPanel.Children.Add(new TextBlock {
                                Text = part + " ", Foreground = message.MessageBrush
                            });
                        }

                        wrapPanel.Children.Add(GetSmiley(_smileys[smileyKey]));

                        messageString = messageString.Substring(index + smileyKey.Length);
                    }
                    else
                    {
                        foreach (string part in messageString.Split(' '))
                        {
                            wrapPanel.Children.Add(new TextBlock {
                                Text = part + " ", Foreground = message.MessageBrush
                            });
                        }
                        break;
                    }
                }

                return(wrapPanel);
            }
            else
            {
                return(null);
            }
        }