如何在 RichTextBox (Windows 手机) 中替换超链接的链接 [英] How to replace link to hyperlink in RichTextBox (Windows phone)

查看:18
本文介绍了如何在 RichTextBox (Windows 手机) 中替换超链接的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一条短信:

Download it directly to the <a href="http://itunes.apple.com/fi/app/goalapp/id502461189?ls=1&mt=8">iTunes Store</a> or <a href="https://market.android.com/details?id=com.strikersoft.meramal.se">Android Market</a>. The application is launched for Swedish app store.

我知道如何在 RichTextBox 中显示超链接:

I know how to show a hyperlink in RichTextBox:

http://msdn.microsoft.com/en-us/library/ee681613%28v=vs.95%29.aspx

但是我怎样才能为这段代码中的每个人制作这个链接?

But how can I make this for everyone link in this code?

推荐答案

您应该使用诸如 RegEx 之类的东西或使用诸如 html 敏捷包.这个扩展方法应该可以工作,让你只需调用richTextBlock.SetLinkedText(htmlFragment);

You should parse the string maybe with something like RegEx or using a library like html agility pack. This extension method should work, letting you just call richTextBlock.SetLinkedText(htmlFragment);

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

public static class RichTextBlockExtensions
{
    public static void SetLinkedText(this RichTextBlock richTextBlock, string htmlFragment)
    {
        var regEx = new Regex(
            @"<as(href=""|[^>]+?shref="")(?<link>[^""]+)"".*?>(?<text>.*?)(</a>|$)",
            RegexOptions.IgnoreCase | RegexOptions.Multiline);

        richTextBlock.Blocks.Clear();

        int nextOffset = 0;

        foreach (Match match in regEx.Matches(htmlFragment))
        {
            if (match.Index > nextOffset)
            {
                richTextBlock.AppendText(htmlFragment.Substring(nextOffset, match.Index - nextOffset));
                nextOffset = match.Index + match.Length;
                richTextBlock.AppendLink(match.Groups["text"].Value, new Uri(match.Groups["link"].Value));
            }

            Debug.WriteLine(match.Groups["text"] + ":" + match.Groups["link"]);
        }

        if (nextOffset < htmlFragment.Length)
        {
            richTextBlock.AppendText(htmlFragment.Substring(nextOffset));
        }
    }

    public static void AppendText(this RichTextBlock richTextBlock, string text)
    {
        Paragraph paragraph;

        if (richTextBlock.Blocks.Count == 0 ||
            (paragraph = richTextBlock.Blocks[richTextBlock.Blocks.Count - 1] as Paragraph) == null)
        {
            paragraph = new Paragraph();
            richTextBlock.Blocks.Add(paragraph);
        }

        paragraph.Inlines.Add(new Run { Text = text });
    }

    public static void AppendLink(this RichTextBlock richTextBlock, string text, Uri uri)
    {
        Paragraph paragraph;

        if (richTextBlock.Blocks.Count == 0 ||
            (paragraph = richTextBlock.Blocks[richTextBlock.Blocks.Count - 1] as Paragraph) == null)
        {
            paragraph = new Paragraph();
            richTextBlock.Blocks.Add(paragraph);
        }

        var run = new Run { Text = text };
        var link = new Hyperlink { NavigateUri = uri };

        link.Inlines.Add(run);
        paragraph.Inlines.Add(link);
    }
}

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        this.richTextBlock.SetLinkedText(
            "Download it directly to the <a href="http://itunes.apple.com/fi/app/goalapp/id502461189?ls=1&mt=8">iTunes Store</a> or <a href="https://market.android.com/details?id=com.strikersoft.meramal.se">Android Market</a>. The application is launched for Swedish app store.");
    }
}

这篇关于如何在 RichTextBox (Windows 手机) 中替换超链接的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆