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

发布于 2024-09-01 03:10:52 字数 13094 浏览 3 评论 0原文

我有一些 HTML,我现在使用 Microsoft 提供的库将其转换为 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>

I have some HTML, which i am converting to XAML using the library provided by Microsoft

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

now, from How to insert XAML into RichTextBox? i am using the following:

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;
}

This however keeps crashing unfortunately =/ Does anyone have any clue on how to display XAML text in a RichTextBox please?

EDIT: Example of html i'm trying to convert

<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 & 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷血 2024-09-08 03:10:52

我假设它在 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;
    }

用法

richTextBox.Document = SetRTF(xamlString);

I assume it crashes on Add() because you are adding a FrameworkContentElement which already has a logical parent. You must first remove a Block from its parent and only then add it to a different collection. Try this:

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;
}

If your XAML already represents a FlowDocument

Then things are much simpler. Just do a:

    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);

随风而去 2024-09-08 03:10:52

或短

    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;
    }

or short

    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;
    }
扶醉桌前 2024-09-08 03:10:52
  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;
  }
  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;
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文