Exemplo n.º 1
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            List <Inline> inlines = new List <Inline>();

            if (null != value)
            {
                MessageWindowTextDataWrapper description = (MessageWindowTextDataWrapper)value;
                Hyperlink hyperlink = MessageLinkHelper.CreateHiperlink(description.Link);
                if (null == hyperlink)
                {   // add simple text
                    inlines.Add(new Run(description.Message));
                }
                else
                {
                    MatchCollection mc = Regex.Matches(description.Message, @"({\d+})");
                    if (0 == mc.Count)
                    {
                        // add simple text
                        inlines.Add(new Run(description.Message));

                        // add link
                        inlines.Add(new Run(" "));
                        inlines.Add(hyperlink);
                    }
                    else
                    {
                        // add text before link
                        string stringLink = mc[0].Value;
                        int    startIndex = description.Message.IndexOf(stringLink, 0);
                        if (0 < startIndex)
                        {
                            inlines.Add(new Run(description.Message.Substring(0, startIndex)));
                        }
                        int position = startIndex + stringLink.Length;

                        // add link
                        inlines.Add(hyperlink);

                        // add text after all links
                        if (position < description.Message.Length)
                        {
                            inlines.Add(new Run(description.Message.Substring(position, description.Message.Length - position)));
                        }
                    }
                }
            }

            return(inlines);
        }
Exemplo n.º 2
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            List <Inline> inlines = new List <Inline>();

            if (null != value)
            {
                MessageDescription description = (MessageDescription)value;

                if (description.IsSimpleMessage)
                {
                    // add simple text
                    inlines.Add(new Run(description.Text));
                }
                else
                {   // add text with link
                    string format = description.Format;
                    try
                    {
                        MatchCollection mc = Regex.Matches(format, @"({\d+})");
                        if (0 == mc.Count)
                        {
                            inlines.Add(new Run(format));
                        }
                        else
                        {
                            int index = 0;
                            for (int i = 0; i < mc.Count; ++i)
                            {
                                // add text before link
                                string stringObj  = mc[i].Value;
                                int    startIndex = format.IndexOf(stringObj, index);
                                if (0 < startIndex)
                                {
                                    inlines.Add(new Run(format.Substring(index, startIndex - index)));
                                }
                                index = startIndex + stringObj.Length;

                                // add link
                                MatchCollection mcNum = Regex.Matches(stringObj, @"(\d+)");
                                if (1 == mcNum.Count)
                                {
                                    int objNum = Int32.Parse(mcNum[0].Value);
                                    if (objNum < description.Objects.Count)
                                    {
                                        MessageObjectContext context = description.Objects[objNum];
                                        string objectName            = string.Format("'{0}'", context.Name);
                                        if (string.IsNullOrEmpty(context.Hyperlink))
                                        {
                                            inlines.Add(new Run(objectName));
                                        }
                                        else
                                        {
                                            Hyperlink hlink = new Hyperlink(new Run(objectName));
                                            hlink.Command = new NavigationCommand(context.Hyperlink, context.Type, context.Id);
                                            inlines.Add(hlink);
                                        }
                                    }
                                }
                            }

                            // add text after all links
                            if (index < format.Length)
                            {
                                inlines.Add(new Run(format.Substring(index, format.Length - index)));
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e);
                        App.Current.Messenger.AddError(e.Message);
                        inlines.Add(new Run(format));
                    }
                }

                // add link
                Hyperlink hyperlink = MessageLinkHelper.CreateHiperlink(description.Link);
                if (null != hyperlink)
                {
                    inlines.Add(new Run(" "));
                    inlines.Add(hyperlink);
                }
            }

            return(inlines);
        }