如何在 wp7 中设置可点击的文本块中的链接 [英] How to set the links in a text block clickable in wp7

查看:27
本文介绍了如何在 wp7 中设置可点击的文本块中的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含链接的文本框.文本中的内容是在运行时生成的.我的问题是文本内的链接不可点击,如何使文本块内的所有链接都可点击,以便当我点击时一个链接它应该打开网络浏览器.在android中我们可以使用自动链接设置它.wp7或wp7.1芒果中是否有这样的选项?

I have a text box contain links .the contents in the text are generated during run time.My problem is that the links inside the text is not clickable,how can make all links inside the text block clickable so that when i tap a link it should open the web browser.In android we can set it by using autolink.Is such option is available in wp7 or in wp7.1 mango?

推荐答案

使用 超链接.

<TextBlock>
    <Run>Pure Text</Run>
    <Hyperlink Command="{Binding HyperLinkTapped}">http://google.com</Hyperlink>
    <Run>Pure Text Again</Run>
</TextBlock>

Windows Phone 7.1 (Mango) 支持此功能.

This is supported from Windows Phone 7.1 (Mango).

如有必要,您可以在运行时根据您的数据创建自己的 FlowDocument.

You can create your own FlowDocument from the your data, at runtime, if necessary.

关于如何从字符串生成 FlowDocument 的示例:

Example on how to generate a FlowDocument from a string:

private void OnMessageReceived(string message)
{
    var textBlock = new RichTextBox()
    {
        TextWrapping = TextWrapping.Wrap,
        IsReadOnly = true,
    };

    var paragraph = new Paragraph();

    var runs = new List<Inline>();

    foreach (var word in message.Split(' '))
    {
        Uri uri;

        if (Uri.TryCreate(word, UriKind.Absolute, out uri) ||
           (word.StartsWith("www.") && Uri.TryCreate("http://" + word, UriKind.Absolute, out uri)))
        {
            var link = new Hyperlink();
            link.Inlines.Add(new Run() { Text = word });
            link.Click += (sender, e) =>
            {
                var hyperLink = (sender as Hyperlink);
                new WebBrowserTask() { Uri = uri }.Show();
            };

            runs.Add(link);
        }
        else
        {
            runs.Add(new Run() { Text = word });
        }

        runs.Add(new Run() { Text = " "});
    }

    foreach (var run in runs)
        paragraph.Inlines.Add(run);

    textBlock.Blocks.Add(paragraph);

    MessagesListBox.Children.Add(textBlock);
    MessagesListBox.UpdateLayout();
}

这篇关于如何在 wp7 中设置可点击的文本块中的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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