コード例 #1
0
 /// <summary>
 /// OnCreateCustomInlineCallback
 /// </summary>
 /// <param name="e">CreateCustomInlineEventArgs</param>
 protected void OnCreateCustomInlineCallback(CreateCustomInlineEventArgs e)
 {
     if (CreateCustomInlineCallback != null)
     {
         CreateCustomInlineCallback(this, e);
     }
 }
コード例 #2
0
        private void UpdateText(string originalText)
        {
            if (string.IsNullOrEmpty(originalText))
            {
                this.Text = "";
                return;
            }

            this.Inlines.Clear();

            bool   isBold             = false;
            string part               = "";
            var    originalTextLength = originalText.Length;

            Brush customForegroundBrush = null;

            int pos1 = 0;

            while (pos1 != -1 || pos1 > originalText.Length - 1)
            {
                int pos2 = originalText.IndexOf('\\', pos1);

                if (pos2 == -1)
                {
                    part = originalText.Substring(pos1);
                    break;
                }

                part = originalText.Substring(pos1, pos2 - pos1);

                if (originalTextLength < pos2 + 2)
                {
                    break;
                }

                char command = originalText[pos2 + 1];

                var run = new Run(part);
                if (isBold)
                {
                    run.FontWeight = FontWeights.Bold;
                }

                if (customForegroundBrush != null)
                {
                    run.Foreground = customForegroundBrush;
                }

                this.Inlines.Add(run);

                if (command == 'n') // new paragraph
                {
                    this.Inlines.Add(new LineBreak());
                    if (originalTextLength > pos2 + 2 && originalText[pos2 + 2] == ' ') // Usually when text is entered in xaml, a new line in the xaml is converted into one space - just skip it
                    {
                        pos2++;
                    }
                }
                else if (command == 'b' || command == '!') // Toggle bold
                {
                    isBold = !isBold;
                }
                else if (command == '*') // Orange square bullet sign
                {
                    var bulletRun = new Run(" ▪ ");
                    bulletRun.Foreground = Brushes.Orange;
                    this.Inlines.Add(bulletRun);
                }
                else if (command == '_')
                {
                    int spacesCount = CountChars(originalText, '_', pos2 + 1);
                    this.Inlines.Add(new Run(new string(' ', spacesCount)));
                    pos2 += spacesCount - 1;
                }
                else if (command == '#')
                {
                    if (originalText[pos2 + 2] == '_')
                    {
                        customForegroundBrush = null;
                        pos2++;
                    }
                    else
                    {
                        string colorHexText = originalText.Substring(pos2 + 2, 6);

                        try
                        {
                            byte red   = Convert.ToByte(colorHexText.Substring(0, 2), 16);
                            byte green = Convert.ToByte(colorHexText.Substring(2, 2), 16);
                            byte blue  = Convert.ToByte(colorHexText.Substring(4, 2), 16);

                            customForegroundBrush = new SolidColorBrush(Color.FromRgb(red, green, blue));
                        }
                        catch
                        {
                            customForegroundBrush = null;
                        }

                        pos2 += 6;
                    }
                }
                else if (command == '\\')
                {
                    // Add backslash
                    this.Inlines.Add(new Run("\\"));
                }
                else if (command == '@')
                {
                    int pos3 = originalText.IndexOf(':', pos2 + 1);
                    int pos4 = originalText.IndexOf('|', pos3 + 1);

                    string anchorText = originalText.Substring(pos2 + 2, pos3 - pos2 - 2);
                    string urlAddress = originalText.Substring(pos3 + 1, pos4 - pos3 - 1);

                    var hyperlink = new Hyperlink(new Run(anchorText));

                    if (urlAddress.Length > 0)
                    {
                        hyperlink.NavigateUri = new Uri(urlAddress);
                    }

                    if (LinkForeground != null)
                    {
                        hyperlink.Foreground = LinkForeground;
                    }

                    if (isBold)
                    {
                        hyperlink.FontWeight = FontWeights.Bold;
                    }

                    hyperlink.RequestNavigate += HyperlinkOnRequestNavigate;

                    this.Inlines.Add(hyperlink);

                    pos2 = pos4 - 1;
                }
                else if (command >= '0' && command <= '9')
                {
                    // Custom Inline action
                    // We trigger CreateCustomInline event and send the index of the commands (0 for '0', 1 for '1')
                    // and text content between start and end command markers.
                    // The event handler should create a custom Inline and set it to CreatedInline property.
                    int endPos = originalText.IndexOf("\\" + command, pos2 + 1); // find end of text for this custom action
                    if (endPos > 0)
                    {
                        var createCustomInlineEventArgs = new CreateCustomInlineEventArgs(customActionIndex: (int)(command - '0'),
                                                                                          contentText: originalText.Substring(pos2 + 2, endPos - pos2 - 2));

                        OnCreateCustomInlineCallback(createCustomInlineEventArgs);

                        if (createCustomInlineEventArgs.CreatedInline != null)
                        {
                            this.Inlines.Add(createCustomInlineEventArgs.CreatedInline);

                            pos1 = endPos + 2;
                            continue;
                        }
                    }
                }

                pos1 = pos2 + 2;
            }

            if (!string.IsNullOrEmpty(part))
            {
                this.Inlines.Add(part);
            }
        }