将XAML转换为FlowDocument以在WPF中的RichTextBox中显示

5

我有一些HTML代码,现在想通过微软提供的库将其转换为XAML。

string t = HtmlToXamlConverter.ConvertHtmlToXaml(mail.HtmlDataString,true);

现在,我从如何将XAML插入RichTextBox?中使用以下内容:

private static FlowDocument SetRTF(string xamlString)
{
    StringReader stringReader = new StringReader(xamlString);
    System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(stringReader);
    Section sec = XamlReader.Load(xmlReader) as Section;
    FlowDocument doc = new FlowDocument();
    while (sec.Blocks.Count > 0)
        doc.Blocks.Add(sec.Blocks.FirstBlock);
    return doc;
}

很遗憾,这个应用程序一直崩溃。请问有人知道如何在RichTextBox中显示XAML文本吗?
编辑:以下是我正在尝试转换的HTML示例。
<FlowDocument xml:space="preserve" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"><Table><TableRowGroup><TableRow><TableCell BorderThickness="1,1,1,1" BorderBrush="Black"><Table><TableRowGroup><TableRow><TableCell BorderThickness="1,1,1,1" BorderBrush="Black"><Table><TableRowGroup><TableRow><TableCell BorderThickness="1,1,1,1" BorderBrush="Black"><Paragraph>Top stories for<LineBreak />Tuesday,<LineBreak />May 4, 2010</Paragraph></TableCell><TableCell BorderThickness="1,1,1,1" BorderBrush="Black"><Paragraph><Hyperlink Foreground="#555555" NavigateUri="http://adverts.timesofmalta.com/openx/www/delivery/ck.php?zoneid=7" /></Paragraph></TableCell></TableRow></TableRowGroup></Table></TableCell></TableRow></TableRowGroup></Table></TableCell></TableRow><TableRow><TableCell BorderThickness="1,1,1,1" BorderBrush="Black"><Table><TableRowGroup><TableRow><TableCell BorderThickness="1,1,1,1" BorderBrush="Black"><Table><TableRowGroup><TableRow><TableCell BorderThickness="1,1,1,1" BorderBrush="Black" RowSpan="10"><Paragraph FontSize="17px">Latest News</Paragraph></TableCell><TableCell BorderThickness="1,1,1,1" BorderBrush="Black"><Paragraph><LineBreak /><Span FontWeight="bold"><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/headlines/20100504/local">News</Hyperlink></Span><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/local/code-breaker">Code breaker</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/local/persuni-li-spiccaw-il-habs-minhabba-kazijiet-ta-vat">106 in prison since 2004 for VAT violations</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/local/mumn-disappointed-over-deployment-of-nurses">MUMN disappointed over deployment of nurses</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/local/press-digest">Press digest</Hyperlink><LineBreak /><LineBreak /><Span FontWeight="bold"><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/headlines/20100504/world-news">World News</Hyperlink></Span><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/world-news/new-iceland-volcano-ash-grounds-flights-in-ireland">Ash cloud returns: Ryanair cancels Malta flight from Dublin</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/world-news/man-arrested-over-ny-bomb-attempt-reports">Man arrested over NY bomb attempt: reports</Hyperlink><LineBreak /><LineBreak /></Paragraph></TableCell></TableRow></TableRowGroup></Table></TableCell></TableRow></TableRowGroup></Table></TableCell></TableRow><TableRow><TableCell BorderThickness="1,1,1,1" BorderBrush="Black"><Table><TableRowGroup><TableRow><TableCell BorderThickness="1,1,1,1" BorderBrush="Black"><Table><TableRowGroup><TableRow><TableCell BorderThickness="1,1,1,1" BorderBrush="Black" RowSpan="10"><Paragraph FontSize="17px">The Times<LineBreak /></Paragraph></TableCell><TableCell BorderThickness="1,1,1,1" BorderBrush="Black"><Paragraph><LineBreak /><Span FontWeight="bold"><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/headlines/20100504/local">News</Hyperlink></Span><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/local/injured-president-returns-from-china">Injured President returns from China</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/local/law-breakers-to-be-barred-from-public-contracts">VAT fraud - Law breakers to be barred from public contracts</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/local/warden-assaulted-on-parking-ticket">Warden 'assaulted' on parking ticket</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/local/against-the-law">Against the law?</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/local/linkin-park-the-kooks-for-isle-of-mtv-concert">Linkin Park, The Kooks for Isle of MTV concert</Hyperlink><LineBreak /><LineBreak /><Span FontWeight="bold"><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/headlines/20100504/world-news">World News</Hyperlink></Span><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/world-news/mumbai-attacks-gunman-convicted">Mumbai attacks gunman convicted</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/world-news/ireland-britain-eye-new-ash-flight-bans">Ireland, Britain eye new ash flight bans</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/world-news/white-male-suspect-linked-to-times-square-car-bomb">White male suspect linked to Times Square car bomb</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/world-news/iranian-president-blasts-us-for-nuclear-threats">Iranian President blasts US for nuclear threats</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/world-news/strikes-to-disrupt-greek-flights-as-from-today">Strikes to disrupt Greek flights as from today</Hyperlink><LineBreak /><LineBreak /><Span FontWeight="bold"><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/headlines/20100504/business">Business &amp; Finance</Hyperlink></Span><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/business/ecb-returns-to-variable-rate-tender-procedures">Money market report - week ended April 30 - ECB returns to variable rate tender procedures</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/business/new-chairman-for-volksbank-malta-ltd">New chairman for Volksbank Malta Ltd</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/business/daily-currency-report">Daily currency report</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/business/financial-news">Financial news</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/business/european-stocks-shake-off-early-jitters-and-gain-ground">Market overview - European stocks shake off early jitters and gain ground</Hyperlink><LineBreak /><LineBreak /><Span FontWeight="bold"><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/headlines/20100504/sport">Sport</Hyperlink></Span><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/sport/valletta-part-with-obrien">Football - Valletta part with O'Brien</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/sport/title-clinching-goal-just-reward-for-ebullient-bajada">Football - BOV Premier League - Title-clinching goal just reward for ebullient Bajada</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/sport/draw-enough-for-sliema-to-secure-third-place">Football - BOV Premier League - Draw enough for Sliema to secure third place</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/sport/bov-premier-league-highlights">Football - BOV Premier League highlights</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/sport/mqabba-finish-on-a-high">Football - Mqabba finish on a high</Hyperlink><LineBreak /><LineBreak /><Span FontWeight="bold"><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/headlines/20100504/opinion">Opinion</Hyperlink></Span><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/opinion/editorial">Editorial - Keeping to the right financial track</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/opinion/habitats-of-the-maltese-islands-7">One World - Habitats of the Maltese islands (7)</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/opinion/guaranteeing-our-heritages-future">Guaranteeing our heritage's future</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/opinion/mixed-data-on-environment">Mixed data on environment</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/opinion/an-issue-of-accountability">An issue of accountability</Hyperlink><LineBreak /><LineBreak /><Span FontWeight="bold"><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/headlines/20100504/letters">Letters</Hyperlink></Span><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/letters/service-deserving-of-a-national-award">Service deserving of a national award</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/letters/vulgar-perception-of-latin-language">Vulgar perception of Latin language</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/letters/chemical-spray-used-on-potato-crops">Chemical spray used on potato crops</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/letters/blasted-from-the-past-1">Blasted from the past (1)</Hyperlink><LineBreak /><Hyperlink Foreground="#555555" NavigateUri="http://www.timesofmalta.com/articles/view/20100504/letters/blasted-from-the-past-2">Blasted from the past (2)</Hyperlink><LineBreak /><LineBreak /></Paragraph></TableCell></TableRow></TableRowGroup></Table></TableCell></TableRow></TableRowGroup></Table></TableCell></TableRow></TableRowGroup></Table></FlowDocument>

它在哪一行失败了?出现了什么异常? - JustABill
在行Section sec上,sec仍然为空,因此在while (sec.Blocks.Count >0 )处崩溃 =/ - Erika
我正在添加一个示例,尝试将其放入RichTextBox中。 - Erika
3个回答

5

我猜测在Add()时程序崩溃是因为你正在添加一个已经有逻辑父级的FrameworkContentElement。你必须先从其父级中移除一个Block,然后再将其添加到另一个集合中。请尝试以下方法:

private static FlowDocument SetRTF(string xamlString)
{
    StringReader stringReader = new StringReader(xamlString);
    System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(stringReader);
    Section sec = XamlReader.Load(xmlReader) as Section;
    FlowDocument doc = new FlowDocument();
    while (sec.Blocks.Count > 0)
    {
        var block = sec.Blocks.FirstBlock;
        sec.Blocks.Remove(block);
        doc.Blocks.Add(block);
    }
    return doc;
}

如果你的XAML已经代表了一个FlowDocument

那么事情就变得更加简单。只需要执行以下操作:

    private static FlowDocument SetRTF(string xamlString)
    {
        StringReader stringReader = new StringReader(xamlString);
        System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(stringReader);
        return XamlReader.Load(xmlReader) as FlowDocument;
    }

Usage

richTextBox.Document = SetRTF(xamlString);


不幸的是,由于“Section sec”仍然为空值,它也会崩溃 =/ 我已经添加了一个我试图转换的示例。 - Erika
3
看起来你的XAML字符串实际上代表了一个FlowDocument。那么事情就简单多了。只需执行return XamlReader.Load(xmlReader) as FlowDocument;,然后就完成了。 - wpfwannabe
哦,太感谢你了。你又救了我一次。当从服务器获取带有HTML标签(<b>,<p>,<i>等)的字符串数据时,这非常重要。 - Shakir Ahmed
谢谢,非常完美!我使用了NuGet包中提供的HtmlToXamlConverter将简单的HTML转换为Xaml字符串,然后使用您的第二个解决方案将其转换为FlowDocument并在RichtTextBox中显示。 - Marco

3

或简称

    private FlowDocument StringToFlowDocument(string s)
    {
        var xamlString = string.Format("<FlowDocument xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"><Paragraph>{0}</Paragraph></FlowDocument>", s);
        return XamlReader.Parse(xamlString) as FlowDocument;
    }

0
  private static FlowDocument SetRTF(string xamlString)
  {
        StringReader stringReader = new StringReader(xamlString);
        System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(stringReader);
        FlowDocument doc = (FlowDocument)XamlReader.Load(xmlReader);
        return doc;
  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接