0

このコードを IsolatedStorage ヘルパーとして使用しています。

public class IsolatedStorageHelper
{
    public const string MyObjectFile = "History.xml";
    public static void WriteToXml<T>(T data, string path)
    {
        // Write to the Isolated Storage
        var xmlWriterSettings = new XmlWriterSettings { Indent = true };
        try
        {
            using (var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var stream = myIsolatedStorage.OpenFile(path, FileMode.Create))
                {
                    var serializer = new XmlSerializer(typeof(T));
                    using (var xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
                    {
                        serializer.Serialize(xmlWriter, data); //This line generates the exception
                    }
                }
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.StackTrace);
            //Dispatcher.BeginInvoke(() => MessageBox.Show(ex.StackTrace));
            //MessageBox.Show(ex.StackTrace);
        }
    }
    public static T ReadFromXml<T>(string path)
    {
        T data = default(T);
        try
        {
            using (var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var stream = myIsolatedStorage.OpenFile(path, FileMode.CreateNew))
                {
                    var serializer = new XmlSerializer(typeof(T));
                    data = (T)serializer.Deserialize(stream);
                }
            }
        }
        catch
        {
            return default(T);
            //add some code here
        }
        return data;
    }
} 

プロパティの 1 つとして ImageSource を持つクラス PdfFile のオブジェクトを保存しています。しかし、オブジェクトの保存中に例外が生成され、System.InvalidOperationException: The type System.Windows.Media.Imaging.BitmapImage was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically. これが何を意味するのか、どのように解決するのかを知りたいというメッセージが表示されます。ありがとうございました

4

2 に答える 2

1

type の変数/プロパティを含むクラスをシリアル化しようとしていますBitmapImage。その型をシリアル化できないため、前述の例外が発生します。名前などをシリアル化し、その情報から逆シリアル化で BitmapImage をインスタンス化することで回避してみてください。

于 2012-06-17T16:41:22.340 に答える