0

我在这个领域进入了全新的领域......

目前,我正在使用 Azure 移动服务和 SQL Azure 数据库来存储我的 Windows Phone 8 应用程序的数据。每次启动应用程序时,它都会通过我设置的一些查询从特定表中提取所有数据。

items = await phoneTable
    .Where(PhoneItem => PhoneItem.Publish == true)
    .OrderBy(PhoneItem => PhoneItem.FullName)
    .ToCollectionAsync();

但是,这并不总是一个很好的做法。我正在尝试实现一种方法,以便在IsolatedStorage加载应用程序时将数据保存到应用程序的 XML 文件中。

我已经获得了一些我认为应该阅读IsolatedStorage并搜索 XML 文件的代码,但我不确定如何下载数据然后将其写入IsolatedStorage.

    public static IEnumerable<Phones> GetSavedData()
    {
        IEnumerable<Phones> phones = new List<Phones>();

        try
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                string offlineData = Path.Combine("WPTracker", "Offline");

                string offlineDataFile = Path.Combine(offlineData, "phones.xml");

                IsolatedStorageFileStream dataFile = null;

                if (store.FileExists(offlineDataFile))
                {
                    dataFile = store.OpenFile(offlineDataFile, FileMode.Open);

                    DataContractSerializer ser = new DataContractSerializer(typeof(IEnumerable<Phones>));

                    phones = (IEnumerable<Phones>)ser.ReadObject(dataFile);

                    dataFile.Close();
                }
                else
                {
                    // Call RefreshPhoneItems();

                }
            }
        }
        catch (IsolatedStorageException)
        {

        }

        return phones;
    }

我正在使用AzureMobileServices SDKandNewtonsoft.Json与数据库进行交互。任何帮助将不胜感激!

4

1 回答 1

0

在这种情况下不要使用ToCollectionAsync- 它会返回一个在绑定到某些 UI 控件时最适合使用的对象。改为使用ToListAsync,有点像下面的代码:

items = await phoneTable
    .Where(PhoneItem => PhoneItem.Publish == true)
    .OrderBy(PhoneItem => PhoneItem.FullName)
    .ToListAsync();

using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
    string offlineData = Path.Combine("WPTracker", "Offline");
    string offlineDataFile = Path.Combine(offlineData, "phones.xml");
    IsolatedStorageFileStream dataFile = null;
    dataFile = store.OpenFile(offlineDataFile, FileMode.Create);
    DataContractSerializer ser = new DataContractSerializer(typeof(IEnumerable<Phones>));
    ser.WriteObject(dataFile, items);
    dataFile.Close();
}
于 2013-07-29T04:57:54.903 回答