2

現在、私は次のような方法を使用しています

    var content = new TextRange(myRichtextBox.NoteBody.Document.ContentStart, myRichtextBox.NoteBody.Document.ContentEnd);
    using (FileStream fs = File.Create(SavePath))
    {
        content.Save(fs, DataFormats.XamlPackage);
    }

私のRichTextBoxのコンテンツを保存するのにうまく機能しています。私が問題を抱えているのは、コントロール全体を保存する方法を見つけることです。

XmlSerializer不変とインターフェイスの継承があるため、このようなオブジェクトをシリアル化するのに問題があるを使用してみました。エラーが発生しますThere was an error reflecting type <my window type>。XmlWriter を使用してみましたが、うまくいきませんでした。

私にとって重要な RTB のいくつかの重要なプロパティと、RTB コンテンツの XAML シリアル化への参照を保存する 2 つ目の XML ファイルを作成することを考えています。しかし、シリアル化できない要素を回避する XML ドキュメントをゼロから作成するというすべての作業を行う前に、知っておきたいことがあります。他にオプションはありますか? RichTextBox の状態を保存するにはどうすればよいですか?

4

1 に答える 1

1

このMSDNフォーラムの投稿によると、名前空間のXamlWriter.Saveメソッドを使用できます。System.Windows.Markup与えられたRichtextBoxの例を変更して、テストしました。

MSDNリンクからのXamlWriterクラスの定義:

提供されたランタイムオブジェクトのXAMLマークアップへの制限付きXAMLシリアル化に使用できる単一の静的Saveメソッド(複数のオーバーロード)を提供します。

Xaml

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <RichTextBox Height="100" HorizontalAlignment="Left" Margin="10,10,0,0" Name="richTextBox1" VerticalAlignment="Top" Width="200" Background="#FF00F7F7">
            <FlowDocument>
                <Paragraph>
                    <Run Text="Hello World"/>
                </Paragraph>
            </FlowDocument>
        </RichTextBox>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="92,164,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <Label Content="Label" Height="221" HorizontalAlignment="Left" Margin="216,12,0,0" Name="label1" VerticalAlignment="Top" Width="250" />
    </Grid>
</Window>

Xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Markup;
using System.IO;
using System.Xml;


namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string savedRichTextBox = XamlWriter.Save(richTextBox1);
            File.WriteAllText(@"C:\Temp\Test.xaml", savedRichTextBox);

            StringReader stringReader = new StringReader(savedRichTextBox);
            XmlReader xmlReader = XmlReader.Create(stringReader);

            RichTextBox rtbLoad = (RichTextBox)XamlReader.Load(xmlReader);

            label1.Content = rtbLoad;
        }
    }
}

Test.xaml

<RichTextBox Background="#FF00F7F7" Name="richTextBox1" Width="200" Height="100" Margin="228,173,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"><FlowDocument PagePadding="5,0,5,0" AllowDrop="True"><Paragraph>Hello World</Paragraph></FlowDocument></RichTextBox>
于 2012-09-11T04:10:03.187 に答える