C# 无法将tye HyperLink的值添加到TextBlock的InlineCollection

C# 无法将tye HyperLink的值添加到TextBlock的InlineCollection,c#,xaml,data-binding,windows-phone-8,windows-phone,C#,Xaml,Data Binding,Windows Phone 8,Windows Phone,我在这段代码中得到了这个错误: <TextBlock> <Run>let me</Run> <Hyperlink NavigateUri="http://www.google.com">google</Hyperlink> <Run>that for you</Run> </TextBlock> 您可以使用dependency属性将“带有超链接的文本”绑定到RichTextBloc

我在这段代码中得到了这个错误:

<TextBlock>
   <Run>let me</Run>
   <Hyperlink NavigateUri="http://www.google.com">google</Hyperlink>
   <Run>that for you</Run>
</TextBlock>
您可以使用dependency属性将“带有超链接的文本”绑定到RichTextBlock
RichEditBox
。它会将每个超链接转换为
超链接按钮
。我建议您自定义
RichEditBox
,使其看起来更像
TextBlock
。还可以自定义
HyperlinkButton
以内联显示

public static class NavigationService
{
    // Copied from http://geekswithblogs.net/casualjim/archive/2005/12/01/61722.aspx
    private static readonly Regex RE_URL = new Regex(@"(?#Protocol)(?:(?:ht|f)tp(?:s?)\:\/\/|~/|/)?(?#Username:Password)(?:\w+:\w+@)?(?#Subdomains)(?:(?:[-\w]+\.)+(?#TopLevel Domains)(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel|[a-z]{2}))(?#Port)(?::[\d]{1,5})?(?#Directories)(?:(?:(?:/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|/)+|\?|#)?(?#Query)(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?#Anchor)(?:#(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)?");

    public static readonly DependencyProperty TextProperty = DependencyProperty.RegisterAttached(
        "Text",
        typeof(string),
        typeof(NavigationService),
        new PropertyMetadata(null, OnTextChanged)
    );

    public static string GetText(DependencyObject d) { return d.GetValue(TextProperty) as string; }

    public static void SetText(DependencyObject d, string value) { d.SetValue(TextProperty, value); }

    static Paragraph paragraph = new Paragraph();
    private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var text_block = d as RichTextBox;
        if (text_block == null)
            return;

        text_block.Blocks.Clear();

        var new_text = (string)e.NewValue;
        if (string.IsNullOrEmpty(new_text))
            return;

        // Find all URLs using a regular expression
        int last_pos = 0;
        foreach (Match match in RE_URL.Matches(new_text))
        {
            // Copy raw string from the last position up to the match
            if (match.Index != last_pos)
            {
                var raw_text = new_text.Substring(last_pos, match.Index - last_pos);
                //text_block.Inlines.Add(new Run(raw_text));
                //var paragraph1 = new Paragraph();
                paragraph.Inlines.Add(new Run { Text = raw_text });
                if (text_block.Blocks.Contains(paragraph))
                {
                    text_block.Blocks.Add(paragraph);
                }
            }

            // Create a hyperlink for the match
            var link = new HyperlinkButton()
            {
                Content = match.Value,
                NavigateUri = new Uri(match.Value),
                Margin = new Thickness(-20, 0, -20, -15),
                FontSize = 20,
                TargetName = "_blank"
            };
            link.Click += OnUrlClick;


            var inlineUi = new InlineUIContainer();
            inlineUi.Child = link;
            if (!paragraph.Inlines.Contains(inlineUi))
            {
                paragraph.Inlines.Add(inlineUi);
            }
            if (text_block.Blocks.Contains(paragraph))
            {
                text_block.Blocks.Add(paragraph);
            }

            //text_block.Inlines.Add(link);

            // Update the last matched position
            last_pos = match.Index + match.Length;
        }

        // Finally, copy the remainder of the string
        if (last_pos < new_text.Length)
        {
            //var paragraph = new Paragraph();
            paragraph.Inlines.Add(new Run { Text = new_text.Substring(last_pos) });
            text_block.Blocks.Add(paragraph);
        }
    }

    private static void OnUrlClick(object sender, RoutedEventArgs e)
    {
        var link = (HyperlinkButton)sender;
        // Do something with link.NavigateUri otherwise by default it would be open in IE.
    }
}

<RichTextBox local:NavigationService.Text="{Binding Review}" IsReadOnly="True" />
公共静态类导航服务
{
//抄袭http://geekswithblogs.net/casualjim/archive/2005/12/01/61722.aspx
(3)协议)(以下::(:::ht)f))tp(s)P(s??::::::::::目前)f)tp(s?:s??::::::://124???????????////124;;)目前目前唯一唯一唯一的非公开静态只读只读只读仅仅仅仅仅仅ReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReReU((((U)))目前目前目前目前目前目前。。。。。URL(URL)URL(URL::::::::::::::::::::::::))新的新ReReReReReReReReReReReReReReReReReReReReReReRe[a-z]{2}](?#端口)(?:[\d]{1,5})(?:(?:/(?:/(?:[-\w~!$+},=]|%[a-f\d]{2}+)+|/)+| \?|?(3)在进行查询时,除以下以下几种::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::(:::::::::::::::::[[[[3-鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于:[-\w~!$+|,*:=]|%[a-f\d]{2})*]?”;
公共静态只读DependencyProperty TextProperty=DependencyProperty.RegisterAttached(
“文本”,
类型(字符串),
类型(导航服务),
新属性元数据(null,OnTextChanged)
);
公共静态字符串GetText(DependencyObject d){返回d.GetValue(TextProperty)作为字符串;}
公共静态void SetText(DependencyObject d,字符串值){d.SetValue(TextProperty,value);}
静态段落=新段落();
私有静态void OnTextChanged(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
var text_block=d作为RichTextBox;
if(text_block==null)
返回;
text_block.Blocks.Clear();
var new_text=(字符串)e.NewValue;
if(string.IsNullOrEmpty(新文本))
返回;
//使用正则表达式查找所有URL
int last_pos=0;
foreach(在RE_URL.Matches中匹配(新文本))
{
//将原始字符串从最后一个位置复制到匹配位置
if(match.Index!=最后位置)
{
var raw_text=new_text.Substring(last_pos,match.Index-last_pos);
//添加(新运行(原始文本));
//var段落1=新段落();
添加(新运行{Text=raw_Text});
if(文本块包含(段落))
{
文本块。块。添加(段落);
}
}
//为比赛创建超链接
var link=新的超链接按钮()
{
Content=match.Value,
NavigateUri=新Uri(match.Value),
余量=新厚度(-20,0,-20,-15),
FontSize=20,
TargetName=“\u blank”
};
link.Click+=OnUrlClick;
var inlineUi=新的InlineUIContainer();
inlineUi.Child=link;
如果(!段落Inlines.Contains(inlineUi))
{
段落.Inlines.Add(inlineUi);
}
if(文本块包含(段落))
{
文本块。块。添加(段落);
}
//文本块。内联线。添加(链接);
//更新最后匹配的位置
last_pos=match.Index+match.Length;
}
//最后,复制字符串的其余部分
如果(最后位置<新文本长度)
{
//var段落=新段落();
Add(新运行{Text=new_Text.Substring(last_pos)});
文本块。块。添加(段落);
}
}
私有静态无效OnUrlClick(对象发送方,路由目标)
{
var link=(HyperlinkButton)发送方;
//使用link.NavigateUri执行某些操作,否则默认情况下它将在IE中打开。
}
}

从中复制和调整代码

尝试添加Target=“\u blank”
中的
。呃……我以为它是
超链接按钮
。你不能使用它,你必须使用
RichTextBlock
。检查一下windows phone中没有
RichTextBlock
RichTextBlock
在windows phone中不存在:|而且该代码是2005年的:))啊,请稍候,我正在转换WP的代码,我希望它能起作用。
public static class NavigationService
{
    // Copied from http://geekswithblogs.net/casualjim/archive/2005/12/01/61722.aspx
    private static readonly Regex RE_URL = new Regex(@"(?#Protocol)(?:(?:ht|f)tp(?:s?)\:\/\/|~/|/)?(?#Username:Password)(?:\w+:\w+@)?(?#Subdomains)(?:(?:[-\w]+\.)+(?#TopLevel Domains)(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel|[a-z]{2}))(?#Port)(?::[\d]{1,5})?(?#Directories)(?:(?:(?:/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|/)+|\?|#)?(?#Query)(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?#Anchor)(?:#(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)?");

    public static readonly DependencyProperty TextProperty = DependencyProperty.RegisterAttached(
        "Text",
        typeof(string),
        typeof(NavigationService),
        new PropertyMetadata(null, OnTextChanged)
    );

    public static string GetText(DependencyObject d) { return d.GetValue(TextProperty) as string; }

    public static void SetText(DependencyObject d, string value) { d.SetValue(TextProperty, value); }

    static Paragraph paragraph = new Paragraph();
    private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var text_block = d as RichTextBox;
        if (text_block == null)
            return;

        text_block.Blocks.Clear();

        var new_text = (string)e.NewValue;
        if (string.IsNullOrEmpty(new_text))
            return;

        // Find all URLs using a regular expression
        int last_pos = 0;
        foreach (Match match in RE_URL.Matches(new_text))
        {
            // Copy raw string from the last position up to the match
            if (match.Index != last_pos)
            {
                var raw_text = new_text.Substring(last_pos, match.Index - last_pos);
                //text_block.Inlines.Add(new Run(raw_text));
                //var paragraph1 = new Paragraph();
                paragraph.Inlines.Add(new Run { Text = raw_text });
                if (text_block.Blocks.Contains(paragraph))
                {
                    text_block.Blocks.Add(paragraph);
                }
            }

            // Create a hyperlink for the match
            var link = new HyperlinkButton()
            {
                Content = match.Value,
                NavigateUri = new Uri(match.Value),
                Margin = new Thickness(-20, 0, -20, -15),
                FontSize = 20,
                TargetName = "_blank"
            };
            link.Click += OnUrlClick;


            var inlineUi = new InlineUIContainer();
            inlineUi.Child = link;
            if (!paragraph.Inlines.Contains(inlineUi))
            {
                paragraph.Inlines.Add(inlineUi);
            }
            if (text_block.Blocks.Contains(paragraph))
            {
                text_block.Blocks.Add(paragraph);
            }

            //text_block.Inlines.Add(link);

            // Update the last matched position
            last_pos = match.Index + match.Length;
        }

        // Finally, copy the remainder of the string
        if (last_pos < new_text.Length)
        {
            //var paragraph = new Paragraph();
            paragraph.Inlines.Add(new Run { Text = new_text.Substring(last_pos) });
            text_block.Blocks.Add(paragraph);
        }
    }

    private static void OnUrlClick(object sender, RoutedEventArgs e)
    {
        var link = (HyperlinkButton)sender;
        // Do something with link.NavigateUri otherwise by default it would be open in IE.
    }
}

<RichTextBox local:NavigationService.Text="{Binding Review}" IsReadOnly="True" />