如何在Windows Phone 8应用程序的独立存储中更新JSON数据对象

时间:2014-04-14 12:12:22

标签: windows-phone-8 json.net isolatedstorage

我有listpicker项目,其中包含费用清单 单击它时,将打开一个新的XAML页面,并获取费用的详细信息 有一个编辑和保存选项。

那么当用户点击更新按钮时,我应该如何更新存储在独立存储中的特定JSON数据对象。

我的代码在这里:

https://onedrive.live.com/redir?resid=83F2A501543779D4%211229

谢谢

以下是UPDATEXPENSES.XAML.CS中的代码。请让我知道我哪里出错了。

导航到此页面后

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e){

        base.OnNavigatedTo(e);
        String dataFromAppSettings;
        ExpencesGroup data;

        NavigationContext.QueryString.TryGetValue("selectedExpenceObject", out selectedIndex);
        NavigationContext.QueryString.TryGetValue("TypeofTransaction", out TransactionType);
        if (TransactionType == ExpencesModel.ExpencesGivenKey)
        {
            if (IsolatedStorageSettings.ApplicationSettings.TryGetValue(ExpencesModel.ExpencesGivenKey, out dataFromAppSettings))
            {
                data = JsonConvert.DeserializeObject<ExpencesGroup>(dataFromAppSettings);

                if (data != null) {
                    thisExpence = data.Expences[Convert.ToInt32(selectedIndex)];
                    this.DataContext = thisExpence;
                    SwitchBetweenListItems(thisExpence.TypeofTransaction);
                    SwitchBetweenListItems(thisExpence.PaidThrough);
                }
            }
        }


        else
        {
            if (IsolatedStorageSettings.ApplicationSettings.TryGetValue(ExpencesModel.ExpencesTakenKey, out dataFromAppSettings))
            {
                data = JsonConvert.DeserializeObject<ExpencesGroup>(dataFromAppSettings);
                if (data != null) {
                    thisExpence = data.Expences[Convert.ToInt32(selectedIndex)];
                    this.DataContext = thisExpence;
                    SwitchBetweenListItems(thisExpence.TypeofTransaction);
                    SwitchBetweenListItems(thisExpence.PaidThrough);

                }
            }
        }

点击更新按钮后

    private void update_Expense(object sender, System.Windows.Input.GestureEventArgs e)
    {
        thisExpence.Amount = amount.Text;
        MessageBox.Show(thisExpence.Amount.ToString());
        thisExpence.ExpenseTitle = ExpenseName.Text;
        ListPickerItem PaidThroughListPicker = (ListPickerItem)paidThrough.SelectedItem;
        thisExpence.PaidThrough = (String)PaidThroughListPicker.Content;
        thisExpence.expenceStatus = (bool)PaymentStatus.IsChecked;
        ListPickerItem listPickerItem = (ListPickerItem)TypeofTransaction.SelectedItem;
        thisExpence.TypeofTransaction = (String)listPickerItem.Content;
        if (thisExpence.TypeofTransaction == ExpencesModel.ExpencesGivenKey)
        {
            var data = JsonConvert.SerializeObject(App.ViewModel.Given);
            IsolatedStorageSettings.ApplicationSettings[ExpencesModel.ExpencesGivenKey] = data;
        }
        else
        {
            var data = JsonConvert.SerializeObject(App.ViewModel.Taken);
            IsolatedStorageSettings.ApplicationSettings[ExpencesModel.ExpencesTakenKey]. = data;
        }
        IsolatedStorageSettings.ApplicationSettings.Save();
        NavigationService.Navigate(new Uri("/ExpencesPage.xaml", UriKind.RelativeOrAbsolute));
    }

1 个答案:

答案 0 :(得分:1)

第1步:您可以从JSON阅读IsolatedStorage数据对象,并使用DataContract将其转换为datacontractjsonserializer。然后,您可以更新按钮单击时的特定值。

步骤2:然后将此DataContract转换回流或Json数据对象并将其存储到IsolatedStorage。

您可以参考This link了解第1步。另请参阅How to: Serialize and Deserialize JSON Data