2

私のコードは以下のようになります:

private void LayoutRoot_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        // TODO: Add event handler implementation here.
        try
        {
            string fileName = NavigationContext.QueryString["id"];
            DataSaver<SampleData> mypersonaldata = new DataSaver<SampleData>();
            var PersonData = mypersonaldata.LoadMyData(fileName);

            txtName.Text = PersonData.Name;
            txtAge.Text = PersonData.Age;
            txtheight.Text = PersonData.Height;
            txtMammals.Text = PersonData.FavMammals;
            txtBirds.Text = PersonData.FavBirds;
            txtReptiles.Text = PersonData.FavReptiles;
        }
        catch
        {
            MessageBox.Show("Unable to read!");
        }
    }

データをロードするための私のコードは次のとおりです。

public MyDataType LoadMyData(string sourceName)
{
    MyDataType retVal = default(MyDataType);
    string TargetFileName = String.Format("{0}/{1}.dat", TargetFolderName, sourceName);
    if (IsoFile.FileExists(TargetFileName))
        using (var sourceStream = IsoFile.OpenFile(TargetFileName, FileMode.Open))
        {
            retVal = (MyDataType)_mySerializer.ReadObject(sourceStream);
        }
    return retVal;
}

DataSaver.cs と SampleData.cs のメンバーにアクセスできます。正しいファイル名も取得していますが、データを読み取ることができません。助けてください!

    public void SaveMyData(MyDataType sourceData) 
    { 
        string TargetFileName = String.Format("{0}/{1}.dat", TargetFolderName, targetFileName); 

        if (!IsoFile.DirectoryExists(TargetFolderName)) 
            IsoFile.CreateDirectory(TargetFolderName); 

        try 
        { 
            using (var targetFile = IsoFile.CreateFile(TargetFileName)) 
            { 
                _mySerializer.WriteObject(targetFile, sourceData); 
            } 
        } 
        catch (Exception e)
        {
            IsoFile.DeleteFile(TargetFileName); 
        }
    }    

上記のコードは、Isolated Storage にデータを保存するためのものです。

4

1 に答える 1

1

これらの 2 つの静的メソッドは、WP 分離ストレージとの間の読み取り/書き込み操作に役立ちます。

public static void WriteStreamToFile(MemoryStream stream, string baseFileName, string extension, out string realFileName)
    {
        realFileName = String.Empty;

        if (stream != null)
            using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                baseFileName = GlobalSettings.NormalizeFileName(baseFileName);

                realFileName = String.Concat(baseFileName, extension);

                using (IsolatedStorageFileStream fileStream = appStorage.OpenFile(realFileName, FileMode.Create, FileAccess.Write))
                    stream.WriteTo(fileStream);

            }
    }

    public static void ReadStreamFromFile(MemoryStream stream, string fileName)
    {
        if (stream != null)
            using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
            using (IsolatedStorageFileStream fileStream = appStorage.OpenFile(fileName, FileMode.Open, FileAccess.Read))
                fileStream.CopyTo(stream);
    }

そして、必要なものすべてをシリアル化できます。

public static object DeserializeObject(string fileName, Type objectType)
    {
        using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
        using (IsolatedStorageFileStream fileStream = appStorage.OpenFile(fileName, FileMode.Open, FileAccess.Read))
        using (TextReader xmlReader = new StreamReader(fileStream))
        {
            XmlSerializer xmlSerializer = new XmlSerializer(objectType);

            return xmlSerializer.Deserialize(xmlReader);
        }
    }

    public static void SerializeObject(string fileName, object target, Type objectType)
    {
        using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
        using (IsolatedStorageFileStream fileStream = appStorage.OpenFile(fileName, FileMode.Create, FileAccess.Write))
        using (TextWriter xmlWriter = new StreamWriter(fileStream))
        {
            XmlSerializer xmlSerializer = new XmlSerializer(objectType);

            xmlSerializer.Serialize(xmlWriter, target);
        }
    }
于 2012-06-26T13:47:22.460 に答える