public static Inline DecorateAsHyperlink(string text) { var hyperlink = new Hyperlink(new Run(text)) { NavigateUri = new Uri(text) }; hyperlink.Click += HyperlinkClickHandler; hyperlink.SetResourceReference(FrameworkContentElement.StyleProperty, "hyperlinkStyle"); return hyperlink; }
/// <summary> /// Renders the specified chat node to the client. /// </summary> /// <param name="node">The node to append.</param> /// <remarks> /// <para>The return value of this function is a reference to the outermost <see cref="Inline">Inline</see> constructed /// by this function. It may create additional inner elements as needed.</para> /// </remarks> /// <returns> /// Returns an object instance of <see cref="Inline">Inline</see> that can be appended to the HTML document. /// </returns> public virtual Inline Render(ChatNode node) { Inline result; if (node == ChatNode.NewLine) { result = new LineBreak(); } else if (node.LinkUri == null) { Run run = new Run(); if (node.CssClass != null) { run.Style = ResourceProvider[node.CssClass] as Style; run.SetResourceReference(FrameworkContentElement.StyleProperty, node.CssClass); } else if (node.Color != GdiColor.Empty) { run.Foreground = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, node.Color.R, node.Color.G, node.Color.B)); } run.Text = node.Text; result = run; } else // need to make a link. { Hyperlink link = new Hyperlink(); link.Inlines.Add(new Run(node.Text)); link.NavigateUri = node.LinkUri; if (node.CssClass != null) { link.SetResourceReference(FrameworkContentElement.StyleProperty, node.CssClass); } else if (node.Color != GdiColor.Empty) { link.Foreground = new SolidColorBrush(WpfColor.FromArgb(255, node.Color.R, node.Color.G, node.Color.B)); } link.ToolTip = string.Format(CultureInfo.CurrentUICulture, "Link to {0}", node.LinkUri.ToString()); result = link; } return result; }
private static void PrepareTweetDocument(TextBlock textBlock, MetroTwitStatusBase tweet, bool Notification = false) { if (DesignerProperties.GetIsInDesignMode((DependencyObject) textBlock)) return; try { if (textBlock != null && textBlock.Inlines != null && textBlock.Inlines.Count > 0) textBlock.Inlines.Clear(); } catch { foreach (Inline inline in Enumerable.ToArray<Inline>((IEnumerable<Inline>) textBlock.Inlines)) textBlock.Inlines.Remove(inline); } if (Notification) { Span span1 = new Span(); span1.FontWeight = FontWeights.SemiBold; Span span2 = span1; Hyperlink hyperlink = new Hyperlink((Inline) new Run("@" + tweet.User.ScreenName)); hyperlink.SetResourceReference(TextElement.ForegroundProperty, (object)"ModernTextLighterBrush"); hyperlink.SetBinding(Hyperlink.CommandProperty, (BindingBase) new Binding("UserProfileCommand") { Mode = BindingMode.OneWay }); hyperlink.SetBinding(Hyperlink.CommandParameterProperty, (BindingBase) new Binding("CurrentTweet.User.ScreenName") { Mode = BindingMode.OneWay }); ((TextElementCollection<Inline>) span2.Inlines).Add((Inline) hyperlink); ((TextElementCollection<Inline>) span2.Inlines).Add((Inline) new Run() { Text = ": " }); ((TextElementCollection<Inline>) textBlock.Inlines).Add((Inline) span2); } if (tweet != null && tweet.Entities != null && tweet.Entities.Count > 0) { try { int[] numArray = StringInfo.ParseCombiningCharacters(tweet.RawText); IOrderedEnumerable<Entity> orderedEnumerable = Enumerable.OrderBy<Entity, int>((IEnumerable<Entity>) tweet.Entities, (Func<Entity, int>) (x => x.StartIndex)); int num1 = 0; foreach (Entity entity in (IEnumerable<Entity>) orderedEnumerable) { int num2 = 0; if (numArray.Length < tweet.RawText.Length && entity.StartIndex < numArray.Length) num2 = numArray[entity.StartIndex] - entity.StartIndex; int num3 = entity.StartIndex + num2; int num4 = entity.EndIndex + num2; int startIndex1 = num1; if (num3 > startIndex1 && startIndex1 < numArray.Length && startIndex1 + (num3 - startIndex1) < numArray.Length) { string input = WebUtility.HtmlDecode(tweet.RawText.Substring(startIndex1, num3 - startIndex1)); if (!string.IsNullOrEmpty(input)) { Run run = new Run(Regex.Replace(input, "\n{2,}|(\r\n){2,}", "\n\n")); ((TextElementCollection<Inline>) textBlock.Inlines).Add((Inline) run); } } if (entity is MentionEntity) { MentionEntity mentionEntity = entity as MentionEntity; if (!string.IsNullOrEmpty(mentionEntity.ScreenName)) { Hyperlink hyperlink = new Hyperlink((Inline) new TagMentionLinkRun("@" + mentionEntity.ScreenName)) { CommandParameter = (object) ("@" + mentionEntity.ScreenName) }; hyperlink.SetResourceReference(TextElement.ForegroundProperty, (object) "HyperlinkUsername"); hyperlink.SetBinding(Hyperlink.CommandProperty, (BindingBase) new Binding("UserProfileCommand") { Mode = BindingMode.OneTime }); ((TextElementCollection<Inline>) textBlock.Inlines).Add((Inline) hyperlink); hyperlink.ContextMenu = new ContextMenu(); MenuItem menuItem1 = new MenuItem(); menuItem1.Header = (object) "Filter tweets from this user"; menuItem1.CommandParameter = hyperlink.CommandParameter; MenuItem menuItem2 = menuItem1; menuItem2.SetBinding(MenuItem.CommandProperty, (BindingBase) new Binding("FilterCommand") { Mode = BindingMode.OneTime }); hyperlink.ContextMenu.Items.Add((object) menuItem2); } } else if (entity is HashTagEntity) { HashTagEntity hashTagEntity = entity as HashTagEntity; if (!string.IsNullOrEmpty(hashTagEntity.Text)) { Hyperlink hyperlink = new Hyperlink((Inline) new TagMentionLinkRun("#" + hashTagEntity.Text)) { CommandParameter = (object) ("#" + hashTagEntity.Text) }; hyperlink.SetResourceReference(TextElement.ForegroundProperty, (object) "HyperlinkHashtag"); hyperlink.SetBinding(Hyperlink.CommandProperty, (BindingBase) new Binding("TagsCommand") { Mode = BindingMode.OneTime }); ((TextElementCollection<Inline>) textBlock.Inlines).Add((Inline) hyperlink); hyperlink.ContextMenu = new ContextMenu(); MenuItem menuItem1 = new MenuItem(); menuItem1.Header = (object) "Filter tweets that contain this hashtag"; menuItem1.CommandParameter = hyperlink.CommandParameter; MenuItem menuItem2 = menuItem1; menuItem2.SetBinding(MenuItem.CommandProperty, (BindingBase) new Binding("FilterCommand") { Mode = BindingMode.OneTime }); hyperlink.ContextMenu.Items.Add((object) menuItem2); } } else if (entity is UrlEntity) { UrlEntity url = entity as UrlEntity; if (!string.IsNullOrEmpty(url.Url)) { if (tweet.AdUrls != null && !string.IsNullOrEmpty(tweet.AdUrls.click_url)) url.ExpandedUrl = tweet.AdUrls.click_url; string str = string.IsNullOrEmpty(url.DisplayUrl) || tweet.AdUrls != null ? url.Url : (url.DisplayUrl.Length > 50 ? url.DisplayUrl.Substring(0, 50) + "..." : url.DisplayUrl); if (tweet.AdUrls != null & !string.IsNullOrEmpty(url.DisplayUrl)) str = url.DisplayUrl; if (url.DisplayUrl == null && tweet.AdUrls == null) { try { MatchCollection matchCollection = RegularExpressions.VALID_URL.Matches(url.Url); if (matchCollection.Count == 0 || matchCollection.Count > 0 && !matchCollection[0].Groups[4].Success) url.Url = "http://" + url.Url; } catch { } } if (tweet.AdUrls != null) { if (string.IsNullOrEmpty(url.DisplayUrl)) url.DisplayUrl = url.Url; url.Url = url.ExpandedUrl; } Hyperlink hyperlink = new Hyperlink((Inline) new TagMentionLinkRun(WebUtility.HtmlDecode(str))) { CommandParameter = (object) url }; hyperlink.SetResourceReference(TextElement.ForegroundProperty, (object) "HyperlinkURL"); hyperlink.SetBinding(Hyperlink.CommandProperty, (BindingBase) new Binding(Notification ? "LinkCommand" : "ContentLinkCommand") { Mode = BindingMode.OneTime }); hyperlink.ToolTip = (object) url.ExpandedUrl; hyperlink.ContextMenu = new ContextMenu(); MenuItem menuItem1 = new MenuItem(); menuItem1.Header = (object) "Copy URL"; menuItem1.CommandParameter = (object) url; MenuItem menuItem2 = menuItem1; menuItem2.SetBinding(MenuItem.CommandProperty, (BindingBase) new Binding("CopyUrlCommand") { Mode = BindingMode.OneTime }); hyperlink.ContextMenu.Items.Add((object) menuItem2); hyperlink.PreviewMouseDown += (MouseButtonEventHandler) ((clickSender, clickArgs) => Application.Current.Dispatcher.BeginInvoke((Action) (() => { string local_0 = url.Url; if (clickArgs.MiddleButton == MouseButtonState.Pressed || Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) { App.LastURLClickMousePosition = new Point?(); CommonCommands.OpenLink(local_0); clickArgs.Handled = true; } else App.LastURLClickMousePosition = new Point?(Application.Current.MainWindow.PointToScreen(Mouse.GetPosition((IInputElement) Application.Current.MainWindow))); }), new object[0])); ((TextElementCollection<Inline>) textBlock.Inlines).Add((Inline) hyperlink); } } num1 = num4; int startIndex2 = num4; if (Enumerable.Count<Entity>((IEnumerable<Entity>) orderedEnumerable) > 0 && Enumerable.Last<Entity>((IEnumerable<Entity>) orderedEnumerable) == entity && startIndex2 < numArray.Length) { string text = WebUtility.HtmlDecode(tweet.RawText.Substring(startIndex2)); if (!string.IsNullOrEmpty(text)) { Run run = new Run(text); ((TextElementCollection<Inline>) textBlock.Inlines).Add((Inline) run); } } } } catch { ((TextElementCollection<Inline>) textBlock.Inlines).Add((Inline) new Run(Regex.Replace(WebUtility.HtmlDecode(tweet.RawText), "\n{2,}|(\r\n){2,}", "\n\n"))); } } else ((TextElementCollection<Inline>) textBlock.Inlines).Add((Inline) new Run(Regex.Replace(WebUtility.HtmlDecode(tweet.RawText), "\n{2,}|(\r\n){2,}", "\n\n"))); }
//private static bool InsertName(ICollection<Inline> textblock, string name) //{ // if (string.IsNullOrEmpty(name)) // return false; // textblock.Add(new Hyperlink(new Run(name))); // textblock.Add(new Run(":")); // return true; //} private static void InsertHyperLink(ICollection<Inline> textblock, Token token) { var ut = token.text; if (string.IsNullOrEmpty(ut)) return; if (ut.Length != token.text.Length) textblock.Add(new Run(" ")); var run = ut.Replace(Properties.Resources.ShortUrlPrefix, string.Empty); //create hyperlink var h = new Hyperlink(new Run(run){FontWeight = FontWeights.SemiBold}) { // NavigateUri = new Uri(ut,UriKind.Absolute),使用这个会直接调用到navigationservice中 Command = WeiZhiCommands.Navigate, CommandParameter = ut, ToolTip = ut, }; h.ToolTipOpening += (s, e) => { var dp =(Hyperlink) s; if (dp.Tag != null) return; var mem = MemoryCache.Default; var ui = (UrlInfo) mem.Get(ut); if (ui == null) return; dp.Tag = ui; if (!string.IsNullOrEmpty(ui.topic())) dp.ToolTip = ui.topic() + " / " + EnumDescription.Get(ui.type); else dp.ToolTip = ui.url_long + " / " + EnumDescription.Get(ui.type); }; h.SetResourceReference(TextElement.ForegroundProperty, "MetroColorFeatureBrush"); textblock.Add(h); var memc = MemoryCache.Default; var uinfo = (UrlInfo)memc.Get(ut); if (uinfo != null) { if (uinfo.type == UrlType.Video) textblock.Add(new Run("\uE173")); else if (uinfo.type == UrlType.Music) textblock.Add(new Run("\u266C")); } else if (ut.Length != token.text.Length) textblock.Add(new Run(" ")); }
private UIElement CreateInfoText(String xmlns, String url) { var textBlock = new TextBlock(); Hyperlink hl = new Hyperlink(new Run(url)); textBlock.Inlines.AddRange(new Inline[] { new Bold(new Run("Prefix: ")), new Run(xmlns), new LineBreak(), new Bold(new Run("Namespace: ")), hl }); // set styles in order to support other // visual studio themes on 2012/2013 var brushKey = VsColors.ToolTipBrushKey; if ( brushKey != null ) { textBlock.SetResourceReference(TextBlock.BackgroundProperty, brushKey); } textBlock.SetResourceReference(TextBlock.ForegroundProperty, VsColors.ToolTipTextBrushKey); hl.SetResourceReference(Hyperlink.ForegroundProperty, VsColors.PanelHyperlinkBrushKey); return textBlock; }