如何保存并打开xaml文件?

问题描述 投票:1回答:1

我正在修补RichTextBox和flowdocuments。

在RichTextBox中格式化文本后,我将使用xaml扩展名保存此文件。

RichTextBox的:

private void Save_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        SaveFileDialog dlg = new SaveFileDialog();
        dlg.Filter = "XAML Format (*.xaml)|*.xaml|All files (*.*)|*.*";
        if(dlg.ShowDialog() == true)
        {
            FileStream fileStream = new FileStream(dlg.FileName, FileMode.Create);
            TextRange range = new TextRange(rtbEditor.Document.ContentStart, rtbEditor.Document.ContentEnd);
            range.Save(fileStream, DataFormats.Xaml);
        }
    }

所以我得到像这样的xaml文件:

<Section>
    <Paragraph>
        <Run>Hello World</Run>
    </Paragraph>
    <Paragraph>
    <Run 
        FontFamily="Showcard Gothic">fdsafdsaf</Run>
    </Paragraph>
</Section>

我能以某种方式将此文件解析为flowdocument吗?我看到this,但我不知道热得到xamlstring。 StreamReader不起作用。我只需要将这个xaml文件包装进去

<FlowDocument></FlowDocument>

解析为flowdocument后。我想做这个:

XAML:

<FlowDocumentScrollViewer x:Name="viewer" />

代码背后:

viewer.document = *MyXamlFileAfterParsing*
c# wpf xaml flowdocument
1个回答
1
投票

我做了这个,它的工作原理:

FileStream xamlFile = File.Open("HUH.xaml", FileMode.Open);            
FlowDocument cds = new FlowDocument((Block)XamlReader.Load(xamlFile));                        
viewer.Document = cds;
xamlFile.Close();
© www.soinside.com 2019 - 2024. All rights reserved.