1

我有一个需要序列化并存储在独立存储设置(wp7)中的自定义子类。我有需要序列化的子类,并且该类扩展了它的基类所以我正在序列化两个类,当序列化类时,我正在使用 [ DataContract] 到类名​​和 [DataMember] 到变量,同时我将所有变量公开,当尝试将数据保存在 IsolatedStorageSettings 中时,我得到了如下异常

数据协定类型 XXXX 不可序列化,因为它不是公共的。公开类型将修复此错误。

所以我用谷歌搜索并进入添加组件的概念

[程序集:System.Runtime.CompilerServices.InternalsVisibleTo("System.Runtime.Serialization")]

在我的类文件中,最后数据对象被保存,但是当停用应用程序时,数据被正确保存,当激活应用程序时,我正在获取对象,但是我恢复的对象中的变量是空状态,我想要获取变量及其值。我是否遗漏了任何导致我保存的对象中的变量为空的内容?

我在下面更新的代码

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Microsoft.Phone.Controls;

using System.Diagnostics;
using System.Runtime.Serialization;
using System.ComponentModel;

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("System.Runtime.Serialization")]

namespace MyApp.Data
{

    [DataContract]
    public class MyDetail : MyDataSet,INotifyPropertyChanged
    {


        public MyDetail (String data)
        {

            ParseResponse(data);
        }

        public MiniStatementDetail(PhoneApplicationPage context)
        {
            _context = context;
        }
        public void performServerRequest(String mobno, String custno, String uid, String AccNo)
        {

            myProtocol request = this.getProtocol(4);               //Pass Service ID
            request.Parameters.append("mobno", mobno);
            request.Parameters.append("custno", custno);
            request.Parameters.append("uid", uid);
            request.Parameters.append("AccNo", AccNo);

            request.Submit();
        }

        public override void ResponseReceived(String response)
        {
           //         
            MyDetail data = new MyDetail (response);
            ResponseReceived(data);
        }



    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(
                this,
                new PropertyChangedEventArgs(propName));
        }
    }
    }
}

以及我在下面添加的子类

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using MyApp.Lib;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using System.Diagnostics;
using MyApp;

using System.Xml.Serialization;//--Added for testing Purpose -Rakesh (10/4/2012)
using System.Runtime.Serialization;
using System.ComponentModel;//--Added for testing Purpose -Rakesh (10/4/2012)

using MyApp.config;

//[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("System.Runtime.Serialization")]

namespace MyApp.data
{
    [DataContract]//--Added for testing Purpose -Rakesh (10/4/2012)
    public class MyDataSet : INotifyPropertyChanged
    {

        [DataMember]
        public List<String[]> _data;
        [DataMember]
        public String[] headers;
        [DataMember]
        public String[] headersAr;
        [DataMember]
        public NameValueCollection _params;
        [DataMember]
        public NameValuePair _params1;
        [DataMember]
        public String _status;
        [DataMember]
        public String _refno;
        [DataMember]
        public String _ptoken;
        [DataMember]
        public String _acid;
        [DataMember]
        public String _branchid;
        [DataMember]
        public String _applicationid;
        [DataMember]
        public String _statusDesc;
        [DataMember]
        public const String HEADER_SEPARATOR = ",";
        [DataMember]
        public const char HEADER_SEPARATOR_SEMI = ';';
        [DataMember]
        public const String ROW_SEPARATOR = "@";

        public event EventHandler OnRequestCompleted;
        [DataMember]
        public PhoneApplicationPage _context;
        [DataMember]
        public NameValueCollection _servicevalues;
        [DataMember]
        public List<String[]> parseddemo;


        public List<String[]> getDataRows()
        {
            return _data;
        }

        public String[] getDataRow(int index)
        {
            return (String[])_data[index];
        }

        public String getToken() { return _ptoken; }

        public String getStatus() { return _status; }


        public String getRefNo() { return _refno; }


        public String[] getHeaders() { return headers; }


        public String getAccountID() { return _acid; }

        public String getApplicationID() { return _applicationid; }

        public String getStatusDesc() { return _statusDesc; }


        public String getHeader(int headerConstant)
        {
            return headers[headerConstant];
        }

        public void ParseResponse(String response)
        {
            App.endResponse = response;
            Debug.WriteLine("Parse Response >>>" + response + "::>>::");
             if (_status.Equals("200"))
            {
                //Common.DisplayAlert(_params.getValueByName("error"));
                LoggedUser.doLogout();
                _context.NavigationService.Navigate(new Uri("/Design/LoginLanding.xaml", UriKind.RelativeOrAbsolute));
                return;
            }
            _statusDesc = _params.getValueByName("StatusDesc");
            _refno = _params.getValueByName("refno");
            _ptoken = _params.getValueByName("ptoken");
            _acid = _params.getValueByName("acid");
            _branchid = _params.getValueByName("branchid");
            _applicationid = _params.getValueByName("ApplicationId");
            _data = ParseResponse(response, "Stmt");



        }

        public List<String[]> ParseResponse(String response, String param)
        {
            List<String[]> data = new List<String[]>();
            String paramValue = _params.getValueByName(param);
            if (!String.IsNullOrEmpty(paramValue))
            {
                String[] dataRows = paramValue.Split(ROW_SEPARATOR.ToCharArray()[0]);
                for (int i = 0; i < dataRows.Length; i++)
                {
                    data.Add(dataRows[i].Split(';'));
                }
            }
            return data;
        }






        public List<String[]> getParamValueBasedData(String paramValue)
        {
            List<String[]> data = new List<String[]>();
            if (paramValue == null) paramValue = "";
            if (!String.IsNullOrEmpty(paramValue))
            {
                String[] dataRows = paramValue.Split(ROW_SEPARATOR.ToCharArray()[0]);
                for (int i = 0; i < dataRows.Length; i++)
                {
                    data.Add(dataRows[i].Split(';'));
                }
            }
            return data;
        }

        public String[] getColumArray(int colid, String firstOption)
        {
            return getColumArray(_data, colid, firstOption);
        }

        public String[] getColumArray(int colid)
        {
            return getColumArray(_data, colid);
        }

        public String[] getColumArray(List<String[]> data, int colid)
        {
            String[] retValues = new String[data.Count];
            for (int i = 0; i < data.Count; i++)
            {
                retValues[i] = ((String[])data[i])[colid];
            }
            return retValues;
        }

        public String[] getColumArray(List<String[]> data, int colid, String firstOption)
        {
            String[] retValues = new String[data.Count + 1];
            retValues[0] = firstOption;
            for (int i = 0; i < data.Count; i++)
            {
                retValues[i + 1] = ((String[])data[i])[colid];
            }
            return retValues;
        }

        public String getParam(String name)
        {
            return _params.getValueByName(name);
        }

        public void OnServerResponse(object sender, EventArgs e)
        {
            myProtocol request = (myProtocol)sender;
            Debug.WriteLine("OnServerResponse::MBDataSet:" + request.Response);
            ResponseReceived(request.Response);
        }

        public myProtocol getmyProtocol(int serviceId)
        {
            myProtocol request = new myProtocol(serviceId);
            request.OnRequestCompleted += new EventHandler(OnServerResponse);
            return request;
        }

        public virtual void ResponseReceived(String response)
        {
        }

        {
            if (OnRequestCompleted != null)
            {
                OnRequestCompleted.Invoke(data, EventArgs.Empty);
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(
                    this,
                    new PropertyChangedEventArgs(propName));
            }
        }
    }

}

忘记了保存和恢复过程。我将 mycustom 对象保存为

var settings = IsolatedStorageSettings.ApplicationSettings;

            if (settings.Contains("SimpleData"))
            {
                settings["SimpleData"] = requiredObj
            }
            else
            {
                settings.Add("SimpleData", requiredObj);

            }

我正在恢复为下面的 gn

  try
           {
               var settings = IsolatedStorageSettings.ApplicationSettings;
               MyDetail demoObj= null;
               if (settings.Contains("SimpleData"))
               {
                   Debug.WriteLine("Data Present");
                   settings.TryGetValue<MyDetail >("SimpleData", out demoObj)
                   requiredObj= demoobj;

               }
               else
               {
                   Debug.WriteLine("Data Absent");
               }

           }
           catch (Exception ex)
           {
               Debug.WriteLine("Exception while retreving datas" + ex);
           }

问题的图片如下 在此处输入图像描述

4

2 回答 2

0

你看过下面的“序列化指南”吗? http://msdn.microsoft.com/en-us/library/6exf3h2k.aspx

属性是否指定为公共的?它包含:“1.CONSIDER 将您的类型的数据成员标记为公共,如果该类型可以在部分信任下使用。在完全信任的情况下,数据合约序列化程序可以序列化和反序列化非公共类型和成员,但只有公共成员可以序列化和反序列化部分信任。”

否则粘贴代码,我们可能会找到答案。此外,最好调试并检查您是否真的存储并从隔离存储中获取一些数据?

于 2012-10-09T14:32:11.217 回答
0

对于任何可以提供帮助的人(请参阅使用 DataContractSerializer 序列化时的 SecurityExceptionhttp://systemmetaphor.blogspot.fr/2010/04/silverlight-serialization-avoiding.html

[assembly: InternalsVisibleTo("System.Runtime.Serialization, PublicKey="
+ "00240000048000009400000006020000002400005253413100040000010001008D56C76F9E86493"
+ "83049F383C44BE0EC204181822A6C31CF5EB7EF486944D032188EA1D3920763712CCB12D75FB77E"
+ "9811149E6148E5D32FBAAB37611C1878DDC19E20EF135D0CB2CFF2BFEC3D115810C3D9069638FE4"
+ "BE215DBF795861920E5AB6F7DB2E2CEEF136AC23D5DD2BF031700AEC232F6C6B1C785B4305C123B"
+ "37AB")]
[assembly: InternalsVisibleTo("System.ServiceModel.Web, PublicKey="
+ "00240000048000009400000006020000002400005253413100040000010001008D56C76F9E86493"
+ "83049F383C44BE0EC204181822A6C31CF5EB7EF486944D032188EA1D3920763712CCB12D75FB77E"
+ "9811149E6148E5D32FBAAB37611C1878DDC19E20EF135D0CB2CFF2BFEC3D115810C3D9069638FE4"
+ "BE215DBF795861920E5AB6F7DB2E2CEEF136AC23D5DD2BF031700AEC232F6C6B1C785B4305C123B"
+ "37AB")]
[assembly: InternalsVisibleTo("System.Runtime.Serialization.Json, PublicKey="
+ "00240000048000009400000006020000002400005253413100040000010001008D56C76F9E86493"
+ "83049F383C44BE0EC204181822A6C31CF5EB7EF486944D032188EA1D3920763712CCB12D75FB77E"
+ "9811149E6148E5D32FBAAB37611C1878DDC19E20EF135D0CB2CFF2BFEC3D115810C3D9069638FE4"
+ "BE215DBF795861920E5AB6F7DB2E2CEEF136AC23D5DD2BF031700AEC232F6C6B1C785B4305C123B"
+ "37AB")]
于 2015-02-11T15:49:21.717 回答