I have a settings save method I call, but I tried unload, and lost focus the application will close out and not save before ever getting to either of those methods. When should I save application settings to keep this from happening?
Should I use a timer and save every 30 seconds, or what?
How often you save depends on your app. However, the key timings are:
Launching
Activated
Deactivated
Closing
Launching is called when the app is first launched from the main screen and Closing is called when the user presses the back key to exit your app. Naturally, you'll most likely want to save permanent data in the Closing event.
Activated is called when the user has closed your app via the Windows button and has gone back into it by pressing the back button. This doesn't get called if the user launches the app for the first time.
Likewise, the Deactivated event is called when the user presses the Windows button. Depending on your app, you'll want to save transient data at this point so that when it's restored, you can give the illusion that your app wasn't closed at all. (Otherwise, for example, all textboxes will become empty even if the user entered data before pressing the Windows button).
Those are the main events, so you can design your app around that. One thing to remember is that if your save files are going to be big, and they take longer than 10 seconds to save after the closing event is called, your app will be terminated immediately, possible corrupting the save file. Therefore, for large saves files, you should plan ahead by saving incrementally (for example, after the user has made a change that should remain permanent).
There's no one size fits all solution to this as saving timings are highly dependant on the type of app being developed. Have a read of the Execution Model MSDN Page as it goes into more detail and provides code examples.
Here is a sample from MSDN on how to implement settings page for Windows Phone.
http://msdn.microsoft.com/en-us/library/ff769510(v=vs.92).aspx
Related
In my first WP7 App, I have all resolved (with MVVM pattern) except for recovering the App from inactivity. I have a main screen with a list of database ítems and a field for each one that, depending on a value and the actual day, shows one value or another (not a calendar, but the same problem).
If the user goes home and, the next day, resumes the App, the calcs must be done again, to refresh the contents. Also, in a second screen happens the same: what it shows depends on the day it is.
How can I detect the activation (I know it is on the Application_Activated function, but don't know how to use it) and refresh all that I need (a refreshInterface function in my second screen, if the user leave the App there, and the main list in the main screen).
I don't need to save nothing in deactivation, just refresh data on activation only.
In your page's OnNavigatedTo method you can subscribe to Application.Activated event (don't forget to unsubscribe in OnNavigatedFrom). In the event handler you can then update the viewmodel.
I am developing an app for windows phone 7.My app communicates with a server which sends it some data which I display on the UI.And I save the state of my app in ApplicationSettings.
And I have found that sometimes
ApplicationSettings.Save() doesnot save the current state.Rather when I start my app it resumes with the previous state which was stored successfully.
Actually I call save method when my app is closing.
But I just want to know if my app is making some changes on the ui.Then is there a possibility that it could be the reason for not saving the state.
If that is the case then what should i do to save my state properly.
If you are saving ONLY when the app is closing (i.e. Application_Closing), then you are not capturing the state often enough. Depending upon how you have exited the app, there are times when the application is simply deactivated, but not necessarily closed. If the app is subsequently tombstoned after deactivation, you will have needed to save the state appropriately to restore it back.
Here is an article to give you a better idea of the different states.
Windows Phone 7 Tombstoning
I read a lot about application states, tombstoning and recommended practises but I am still confused
Here is my scenario
User launches app through application icon.
User then moves to next page.
User Click on Windows button.
User launches app again using application icon
What should happen here?
My second page should be displayed? because user didn't quit the app through back button
Main page should be displayed? because user launched it through app list which means new instance
MSDN says
Ensure that when your application is launched by the user from Start
or the installed applications list, the user is taken to a consistent
launch experience. It should be evident to the user that they are
experiencing a new application instance.
Does this mean that I should launch the main page again not second page?
If yes, should my app state only be restored(to second page) when user comes to my app through back button and Application_Activated event is fired?
You should start new instance of application.
I believe this is also part of certification process.
And the whole thing would get quite complicated, because only way to ensure, that application is onpened on same page, with same data, is to save whole state into isolated storage.
Unless you're doing some crazy custom stuff, this should be handled for you as page navigation is supported by the deactivation process.
If the user backs into your application, the last page they were on will be restored and PhoneApplicationService.Activated will be raised (ActivatedEventArgs.IsApplicationInstancePreserved will be true if you don't need to unpersist your state).
If the user accesses your application from start, the application's default page will be loaded and PhoneApplicationService.Launching will be raised.
The only thing I would show first one time only is the help or options screen so a new user can get to know the app.
From that time on, when the app is started it should start with the key/main page that the user wants to use.
I am having a very difficult time trying to debug/fix an application.
Briefly:
- I created a "wizard" type app that starts with the user taking a photograph (using the common dialog for photos)
If the user tries to use the text input window (SIP) (the little keyboard input window) after a photo is taken the event loop seems to hang - the event is not processed or is delayed for a while.
If the user does not take a picture the SIP keyboard works great.
This only happens on some of my devices. Specifically it is not a problem on an MC65 but is a problem on an ES400.
It appears that the app's event loop gets screwed up with the way I am displaying forms and taking photos.
If created a simple test app with single form containing a button (Event handler takes a photo) and a text box that accepts input. That works fine. But it is only a single form app that does nothing else.
When I combine the photo taking with my form displaying (making a "wizard" ) things go badly.
I wonder what kind of event loop should I be running?
Essentially the user takes a photo then goes through some forms (I hide one form and show another when they click the "next" button.)
The Form.Show is called from the main form after a picture is taken and then I have something like:
while(UserNotFinished)
{
Application.DoEvents()
}
Where UserNotFinished is a flag set from my wizard/forms after the "submit" button is pressed.
I will be happy to provide more code but not sure what would be useful.
I am new to C# and CF development (lots of years of C++/Win32)
The real confusing part is that this works on one device but not on another. In fact, the device hangs completely. It ends the activesync connection and sometimes I have to hard reset by removing the battery.
I think your problem stems from the while(true) { DoEvents(); } and perhaps how you are trying to go between forms. The only time I've used the DoEvents() method is when I'm already in the scope of a windows event and I need to be sure something in the message queue is processed so screen updates are correct. I'd suggest making a controller class to manage the screen flow for your wizard. You can control the screen flow by either using ShowDialog() and execute the flow control directly in the scope of a single call, or you'll have to use Show() and an asynchronous mechanism such as subscribing to and handling specific form and control events in the controller class. Also saw the comment about introducing another thread, beware that Forms belong to the thread they were created in and you must Invoke(...) all Form members in the context of the creating thread.
Hmm. Very strange
I started a new thread and basically call Application.DoEvents() in in as well and it seems to fix the problem...
I don't know why the
while(true)
{
DoEvents()
}
in the main thread doesn't work.
I know about tombstoning stuff any how to save the states of your application but my question is this regarding states.
Let's say I have a page that have 5 buttons. Whenever I click a button the button can be either visible or hidden. How do I save the current state or the saved state of my page? Let's say if I exit my application and loaded the 5 buttons again one button should be hidden.
I tried saving a variable state value in an IsolatedStorageFile and retrieve it when the Page is loaded in loaded event but I figured out that it's too tedious for the processor to do a lot of things just for saving a state.
Another solution I thought of is when the Loaded event is fired, I will statically declare an if else statements and manually declare the Visibility of the button.
Is there any other way?
Are you trying to do the same thing when the page/app is exited normally and when it's tombstoned? The default expected behaviour is that when launching the app after exiting (via the back button) the app will be in it's default state. When returning from a tombstoned state the app should (as far as is appropriate) be displayed to the user in exactly the same way as when the page/app was tombstoned.
Of course you may have differing needs but I wanted highlight the standard behaviour.
If you want to store state while tombstoned, the convention is to use the OnNavigatedFrom and OnNavigatedTo events to store details in the State object.
There is a good example of how to do this on MSDN: How to: Preserve and Restore Page State for Windows Phone
If you want to preserve state across all executions of your app then you will need to use IsolatedStorage to store the details in a persistent location. Where and when you read and write the data will depend on where the data you need to persist is located.
If it's at an applicaiton level you may be able to use the application level events (Launching, Activated, Closing & Deactivated). If you have state data at page level it's probably be better to do it in the page level (Loaded/Unloaded or NavigatedFrom/To as appropriate).
If you want to store state across all executions of the application you probably need to look at both of the above options.
The System.IO.IsolatedStorage namespace is fine for this kind of behaviour, check it out here
Also check out this article on how to do what you are asking.
Saving Applications States