wp7:应用失败了!想不通哪里?

时间:2011-04-03 15:34:33

标签: visual-studio-2010 windows-phone-7

我想我已经将它缩小到Deactivation事件中的代码:

这就是......当我在这个代码中设置一个断点时,一切正常。应用程序不会失败。但是,当我取消断点时,它失败了。我不明白为什么try / catch没有被解雇。

我还应该注意到,我在没有断点的情况下对此事件进行了评论,应用程序运行良好。所以这是代码中的内容......

可能是未保存对象的保存事件未完成,并且当它尝试重新激活激活事件时实际上是失败的那个???

private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
    //MessageBox.Show("deactivated");
    try
    {
        //if ((Application.Current as App).infoSaved == false)
        //{
        unsaved unSavedPillInfo = new unsaved();
        unSavedPillInfo.RXName = (Application.Current as App).appRXName;
        unSavedPillInfo.RXNumber = (Application.Current as App).appRXNumber;
        unSavedPillInfo.DosageNotes = (Application.Current as App).appDosageNotes;
        unSavedPillInfo.Generic = (Application.Current as App).appGeneric;
        unSavedPillInfo.Instructions = (Application.Current as App).appInstructions;
        unSavedPillInfo.Reason = (Application.Current as App).appReason;

        unSavedPillInfo.Quantity = (Application.Current as App).appQuantity;
        unSavedPillInfo.Refills = (Application.Current as App).appRefills;

        unSavedPillInfo.Doctor = (Application.Current as App).appDoctor;
        unSavedPillInfo.DoctorNumber = (Application.Current as App).appDoctorNumber;
        unSavedPillInfo.Pharmacy = (Application.Current as App).appPharmacy;
        unSavedPillInfo.PharmacyNumber = (Application.Current as App).appPharmacyNumber;

        unSavedPillInfo.OrigDate = (Application.Current as App).appOrigDate;
        unSavedPillInfo.ReorderReminder = (Application.Current as App).appReorderReminder;
        unSavedPillInfo.ReorderDate = (Application.Current as App).appReorderDate;
        unSavedPillInfo.ConsumptionFrequency = (Application.Current as App).appConsumptionFrequency;

        unSavedPillInfo.PerscriptionUpdated = (Application.Current as App).perscriptionUpdated;
        unSavedPillInfo.PerscriptionUpdated = (Application.Current as App).doctorUpdated;
        unSavedPillInfo.PerscriptionUpdated = (Application.Current as App).detailsUpdated;
        unSavedPillInfo.PerscriptionUpdated = (Application.Current as App).pharmacyUpdated;

        unSavedPillInfo.Save();
        //}
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }

}

2 个答案:

答案 0 :(得分:2)

这并不完美,但尝试在每个事件处理程序中放入一个Messagebox。通过这种方式,您可以判断每个人何时开火并查看是否未开火。

此外,您可能希望经常卸载应用程序以清除IsolatedStorage。如果您继续在同一安装上运行,则会产生问题。

编辑:是的,从我遇到的情况来看,如果您没有正确保存到隔离存储,应用程序可能会挂起。如果您没有从隔离存储中正确加载数据,也会发生这种情况。你可能想分别尝试每一个。使用消息框确保它正确保存和加载,因为VisualStudio将退出当前的调试会话。

UPDATE 您应该做的是创建一个名为unsavedPrescription的全局变量。现在,当用户选择处方时,将全局变量“未保存”分配给他们选择的处方。注意:当应用程序停用时,您不应该分配属性,因为它很可能无法完全保存,导致应用程序挂起。相反,您只需将选定的处方分配给全局变量,并将App.xaml.cs中的代码更改为以下内容:

public unsaved unsavedPrescription {get; set;}

private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
    //Open up IsolatedStorageSettings
    IsolatedStorageSettings settings = Isolated StorageSettings.ApplicationSettings;

    //Use a model to save prescription
        //So create a name/value pair to store the prescription in isolatedstorage
    //Notice we used the global variable
    settings["UnsavedPrescription"] = unsavedPrescription;
}

private void Application_Activated(object sender, ActivatedEventArgs e)
{
    //Now you can easily load the prescription you saved
    //I'm reassigning the global variable that will contain the savedprescription

    if(settings.TryGetValue("UnsavedPrescription", out prescription)
    {
        unsavedPrescription = prescription;
    }
}

这极大地简化了加载和保存时的代码。你也可以像我之前所说的那样使用消息框进行测试,这不是专业的但是效果很好。当应用程序尝试停用时,您还没有运行太多。这种方式将在我测试时起作用。你上面这样做的方式看起来就像你在应用程序停用时运行了很多代码,这可能就是它挂起的原因。这也解释了为什么当你删除它时,一切正常。

答案 1 :(得分:0)

我遇到的问题与你的问题类似,实际上与IsolatedStorage本身的保存/加载无关,而是我设置/获取的属性具有无限递归。这导致应用程序在获取Catch语句之前终止。

关闭调试器单步执行属性可能是值得的: 工具 - >选项 - >调试器 - >跳过属性和运算符(仅管理)

public Dictionary<string, object> Dictionary
        {
            get
            {
                if (_dictionary == null)
                    _dictionary = new Dictionary<string, object>();
                return _dictionary;
            }
            set
            {
                Dictionary = value;
            }
        }

App Never launches successfully again after Tombstoning. No exception thrown