Timer and saving its state - windows-phone-7

I am building a windows phone application (basically its a game but I am not using XNA, Silverlight was enough). The graphics are moving based on a DispatcherTimer. What I want to do is basically stop the timer whenever a call arrives on the phone, and start it again after the call has finished, so that the game state is not lost.
I tried with :
// Code to execute when the application is activated (brought to foreground)
// This code will not execute when the application is first launched
private void Application_Activated(object sender, ActivatedEventArgs e)
{
Game.timer.Start();
}
// Code to execute when the application is deactivated (sent to background)
// This code will not execute when the application is closing
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
Game.timer.Stop();
}
but it did not work, it actually does not reach to this point when a call arrives at the phone. Anyone had such experience?
Thanks in advance :)

When a call is received you will receieve the Obscured Event on the Frame.
Please note that this event can also be fired for more than just a received phone call though.

Related

Starting a WCF service in Windows Service when initialization takes a long time

I have a WCF project that I host in a windows Service (actually I use topshelf) and it runs fine. I have to make changes however that will cause the initialization to take substantially longer. This causes problems when the service starts as it times out while executing the WCF Service constructor.
I wanted to reduce the amount of code in the constructor and then when the service was opened I would do my longer running initializations. I put register the open event in the WCF ctor but it does not seem to be called.
public WCFService()
{
this.Faulted += WCF_Faulted;
this.Opened += WCF_Opened;
...
and event handlers
void WCF_Opened(object sender, EventArgs e)
{
}
void WCF_Faulted(object sender, EventArgs e)
{
}
I am guessing that I am not implementing this correctly.
I was able to trap the opened event in the servicehost but then I dont know how to access the instance (it is a singleton) to call a method on it.
Ideas?

How to make a synchronous web call from within a background agent

I am fairly new to wp7 development, and currently developing an app that has a background agent to update values based upon responses it gets from a web call to an api.
My problem is that the response to the web call is an asynchronous call and I can not access the results returned from within the background agent.
Is there any way that i can make a synchronous call from within the background agent so as to allow me to deal with the results within the same agent?
I have tried dealing with the web call within a class in a shared library but the Asynchronous call is only made after the onInvoke method of the agent has finished and so no use. Any Ideas would be great.
You simply need to call the NotifyComplete() method in your async call's Completed handler, and not before. Remove the call at the end of Invoke.
you could use an AutoResetEvent like this:
protected override void OnInvoke(ScheduledTask task)
{
AutoResetEvent are = new AutoResetEvent(false);
//your asynchronous call, for example:
WebClient wc = new WebClient();
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
wc.OpenReadAsync(searchUri, channel);
// lock the thread until web call is completed
are.WaitOne();
//finally call the NotifyComplete method to end the background agent
NotifyComplete();
}
and your callback method should look like:
void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
//do stuff with the web call response
//signals locked thread that can now proceed
are.Set();
}
remember that you should check if a connection is available and handle possible exceptions, if your background agent gets killed twice consecutively (due to memory consumed or duration) it will be disabled by the OS.

Windows phone: close the app after click on a button

How can I close my app after a button click? I mean in:
private void chiudi_app(object sender, RoutedEventArgs e){
}
I've tried a lot of things founded around the web (like Application.Exit() and other things), but without success.
In WP7 there is no API to exit your app. But you can create an ExitException
public class ExitException :Exception {}
and throw it where ever you want to quit.
throw new ExitException();

How can i use NavigationServices in Application_Activated windows phone 7?

im developin an app for wp7 that it holds pictures and notes with password login. But when app running if user press windows button app is running at background and if user press back button it resumes without asking password again.
i tried to Navigate when app activated but i couldnt manage it in Application_Activated method. is there a way to do that? Or could you advice me sth else that solve my problem.
ty.
here is my code im using to navigate,
(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
I got around this issue by using UserControls on the MainPage, showing one if the user had not yet logged in and the other if they had, I set these controls up to show/hide based on certains states in the MainPage and then bind that to the MainViewModel:
private void Application_Activated(object sender, ActivatedEventArgs e)
{
// Ensure that application state is restored appropriately
....your code here to load stuff...
App.ViewModel.MainPageState = "ShowThemTheLogin";
}
}

Events Deactivated and Activated only work once

When I am running the application and press the start button, the program runs the deactivated event, and If I back to the game, it runs the activated. But if then I press the start button, It doesn't runs the deactivated again. What's up?
I haven't tested it myself but it sounds like your event handler for "Deactivated" only gets hooked once (on application startup) and not when the application is (re)activated.
Where do you register your event handlers for activated/deactivated?
I guess you've already seen it but here is a good tutorial by Shawn Wildermuth:
Tombstoning
If it doesn't work with above guidelines, could you perhaps try to hook the Deactivated event yourself from the Activated event in code behind?
void Application_Activated(object sender, ActivatedEventArgs e)
{
Deactivated += Application_Deactivated;
}
I don't think that above "hook" should be necessary though. Please post some code of how you register the events and the event handlers if you're still unsure.
HTH

Resources