wp7:应用失败!想不通在哪里? [英] wp7: App failing! Can not figure out where?

查看:26
本文介绍了wp7:应用失败!想不通在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我已经将其缩小到停用事件中的这段代码:

I think I've narrowed it down to this code in the Deactivation event:

事情就是这样......当我在这段代码中放置一个断点时,一切正常.应用程序不会失败.但是,当我取消断点时,它失败了.我不明白的是为什么 try/catch 没有触发.

Here's the thing... When I put a break point in this code everything works fine. The application does NOT fail. However, when I take the break point off it fails. What I don't understand is why the try/catch isn't firing.

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

I should also note that I commented everything out of this event with no break point and the application worked fine. So it is something in this code...

可能是因为未保存对象的保存事件没有完成,而当它试图重新激活激活事件时,实际上是失败了???

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());
    }

}

推荐答案

这并不完美,但可以尝试在每个事件处理程序中放置一个 Messagebox.这样你就可以知道每个人什么时候开火,看看一个人是否没有开火.

This is hardly perfect but try putting a Messagebox inside of each of the eventhandlers. That way you can tell when each one is firing and see if one isn't firing.

此外,您可能希望经常卸载应用程序以清除隔离存储.如果您继续在同一个安装上运行,这会产生问题.

Also you might want to unistall the application often to clear the IsolatedStorage. This is known to create issues if you keep running on the same installation.

是的,根据我遇到的情况,如果您没有正确保存到隔离存储,应用程序可能会挂起.如果您没有从独立存储正确加载数据,也会发生这种情况.您可能想单独尝试每一种.使用消息框确保它正确保存和加载,因为 VisualStudio 将退出当前调试会话.

Yeah from what I have run into, the application can hang if you aren't properly saving to isolated storage. It can also happen if you aren't properly loading data from isolated storage. You might want to try each one seperately. Use a messagebox to make sure it saves and loads properly because VisualStudio will exit the current debugging session.

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

UPDATE What you should do is create a global variable called unsavedPrescription. Now when the user selects a prescription assign the global variable "unsaved" the prescription they selected. Note: you should not be assigning properties when the app is deactivating because it is very possible it won't save completely which leads to the app hanging. Instead all you have to do is assign the selected prescription to the global variable and change your code in App.xaml.cs to the following:

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;
    }
}

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

This greatly simplifies your code when loading and saving. Also you'll be able to test using the messageboxes like I said earlier, which isn't professional but it works nicely. Also your not running too much stuff when the app is trying to deactivate. THIS WAY WILL WORK AS I TESTED IT. The way you did it above looks like your running to much code while the app is deactivating which is probably why its hanging. Also this explains why when you remove it, everything works.

这篇关于wp7:应用失败!想不通在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆