Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法将运行添加到foreach循环中的textblock_C#_Xaml_Uwp - Fatal编程技术网

C# 无法将运行添加到foreach循环中的textblock

C# 无法将运行添加到foreach循环中的textblock,c#,xaml,uwp,C#,Xaml,Uwp,我正在构建一个UWP应用程序。 我试图实现的是显示一条推文,如果推文中有任何URL,则将其呈现为超链接文本。 所以我要做的是遍历文本,查找URL,并将运行分配到textblock,然后将其分配到页面上的textblock 代码隐藏: TextBlock block = new TextBlock(); Regex url_regex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)" , RegexOptions.IgnoreCase | Rege

我正在构建一个UWP应用程序。 我试图实现的是显示一条推文,如果推文中有任何URL,则将其呈现为超链接文本。 所以我要做的是遍历文本,查找URL,并将运行分配到textblock,然后将其分配到页面上的textblock

代码隐藏:

TextBlock block = new TextBlock();

        Regex url_regex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)" , RegexOptions.IgnoreCase | RegexOptions.Compiled);

        MatchCollection collection = url_regex.Matches(tweet);

        int index = 0;

        //for test only
        Run r = new Run();
        r.Text = "int";
        block.Inlines.Add(r);

        foreach (Match item in collection)
        {

            Run run = new Run();
            run.Text = tweet.Substring(index , item.Index);
            //error occurs here.
            block.Inlines.Add(run);

            index = item.Index;

            run.Text = tweet.Substring(index , item.Length);
            Hyperlink h = new Hyperlink();
            h.Inlines.Add(run);
            block.Inlines.Add(h);

            index = item.Index + item.Length;
        }

        r.Text = tweet.Substring(index , tweet.Length);
        block.Inlines.Add(r);

        blok = block;
Xaml:

<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBox Name="input"
                PlaceholderText="input here" />
    <TextBlock Name="blok"/>
</StackPanel>

互联网上还有关于这个主题的其他问题,但我没有找到一个好的解决方案。

您正在尝试将相同的
运行
元素分配给两个家长:
文本块
超链接

Run run = new Run();
run.Text = tweet.Substring(index , item.Index);
//error occurs here.
block.Inlines.Add(run);

index = item.Index;

run.Text = tweet.Substring(index , item.Length);
Hyperlink h = new Hyperlink();
h.Inlines.Add(run);
block.Inlines.Add(h);

index = item.Index + item.Length;
虽然这是两个不同的运行,但请将循环更改为:

foreach (Match item in collection)
{

    Run runRegularText = new Run();
    runRegularText.Text = tweet.Substring(index, item.Index);
    block.Inlines.Add(runRegularText);

    index = item.Index;

    Run runHyperlink = new Run();
    runHyperlink.Text = tweet.Substring(index, item.Length);
    Hyperlink h = new Hyperlink();
    h.Inlines.Add(runHyperlink);
    block.Inlines.Add(h);

    index = item.Index + item.Length;
}

您正在尝试将相同的
Run
元素分配给两个父元素:
TextBlock
Hyperlink

Run run = new Run();
run.Text = tweet.Substring(index , item.Index);
//error occurs here.
block.Inlines.Add(run);

index = item.Index;

run.Text = tweet.Substring(index , item.Length);
Hyperlink h = new Hyperlink();
h.Inlines.Add(run);
block.Inlines.Add(h);

index = item.Index + item.Length;
虽然这是两个不同的运行,但请将循环更改为:

foreach (Match item in collection)
{

    Run runRegularText = new Run();
    runRegularText.Text = tweet.Substring(index, item.Index);
    block.Inlines.Add(runRegularText);

    index = item.Index;

    Run runHyperlink = new Run();
    runHyperlink.Text = tweet.Substring(index, item.Length);
    Hyperlink h = new Hyperlink();
    h.Inlines.Add(runHyperlink);
    block.Inlines.Add(h);

    index = item.Index + item.Length;
}

block.Inlines.Add(r)已执行两次。。。循环之前和之后各一次。。。这就是为什么吗?在测试中添加一个之前没有。。正如我所猜测的,在foreach块之外添加它多少次都没有错误。而且它只在foreach循环内部出现一次错误。您是否尝试过使用构造函数创建Run类和init?i、 e run=new run(strText)?在循环中,您还将相同的运行实例
run
添加到超链接元素的内联线中。那不行。您必须创建一个新实例。block.Inlines.Add(r)已执行两次。。。循环之前和之后各一次。。。这就是为什么吗?在测试中添加一个之前没有。。正如我所猜测的,在foreach块之外添加它多少次都没有错误。而且它只在foreach循环内部出现一次错误。您是否尝试过使用构造函数创建Run类和init?i、 e run=new run(strText)?在循环中,您还将相同的运行实例
run
添加到超链接元素的内联线中。那不行。您必须创建一个新实例。