在墓碑发生时存储变量

时间:2013-12-06 06:10:40

标签: windows-phone-8

如何将变量存储在TombstoneHelper类的savestate()方法中以避免Windows Phone 8中的Tombstone。

2 个答案:

答案 0 :(得分:0)

我认为您可以尝试使用PhoneApplicationService.State属性 - 它的工作方式类似于Dictionary(字符串,对象)。 MSDN

要保存已停用的事件:

private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
    int myObject = 5;
    PhoneApplicationService.Current.State.Add("myName", myObject);
}

要恢复已激活的事件:

private void Application_Activated(object sender, ActivatedEventArgs e)
{
    var returnedOnject = PhoneApplicationService.Current.State["myName"];
}

答案 1 :(得分:0)

谢谢男人+1给你的评论Romasz。它现在正在工作,但是当应用程序进入逻辑删除阶段时出现错误,因为当你的应用程序来自Tombstone Stage时,PhoneApplicationService State为null。所以我将phoneState存储在隔离application_Deactivated中的存储和从application_activated中的独立存储中检索。 我创建了一个简单的LoginData类,我可以存储我必须保存的数据。

[DataContract]
    public class LoginData
    {

        [DataMember]
        public string Username { get; set; }
        [DataMember]
        public string Password { get; set; }
        [DataMember]
        public int EstablishmentId { get; set; }
        [DataMember]
        public bool chainmanager { get; set; }
        [DataMember]
        public string logoimage { get; set; }

        public LoginData(string username, string password, int establishmentId, bool ischainmager,string logoimage)
        {
            this.Username = username;
            this.Password = password;
            this.EstablishmentId = establishmentId;
            this.chainmanager = ischainmager;
            this.logoimage = logoimage;

        }
    }

然后make方法在App.xml中保存Class对象

 public void save(LoginData ld)
        {
            PhoneApplicationService phonestate = PhoneApplicationService.Current;

                phonestate.State["_lOGINcREDENTIALs_"]= ld;
                var settings = IsolatedStorageSettings.ApplicationSettings;
                settings["Credatials"] = phonestate.State["_lOGINcREDENTIALs_"];

        }

加载Class对象的方法

public void load()
        {
            PhoneApplicationService phonestate = PhoneApplicationService.Current;
            var settings = IsolatedStorageSettings.ApplicationSettings;
            object credentials;
            if (settings.TryGetValue("Credatials", out credentials))
            {

                phonestate.State["_lOGINcREDENTIALs_"]= credentials;
            }

        }

并在各种事件中调用这些方法,如

 private void Application_Launching(object sender, LaunchingEventArgs e)
    {
        load();
    }
 private void Application_Activated(object sender, ActivatedEventArgs e)
  {
        load();
  }
 private void Application_Deactivated(object sender, DeactivatedEventArgs e)
  {
       save();
  } 
 private void Application_Closing(object sender, ClosingEventArgs e)
  {
      save();
  }