Exit an app

9 views
Skip to first unread message

Gerhard Kreuzer

unread,
Feb 22, 2011, 2:51:11 AM2/22/11
to nro...@googlegroups.com
Hi,
 
I have nearly done my 'Proof of Concept' app, as far as nRoute is involved, but there is still a point open.
 
How can I stop the app?
 
I take a look at the FuturDesktop app and as far as I found out, there is a button and within it's event handler in the code behind file (==UI layer) a the Close-method of the main window is called.
 
First of all, there will be some code necessary to safely close down an app, so this button should call a method of the associated VM first.
Second, I want to close down my app safely, after some time of hanging around without usage. Its also a security option we have to take. So I need a way to close down programatically.
Third, the concept, that views depending on VM's but NOT other way round is the big advantage of MVVM over other patterns, so I would not break this by adding some hack which allows me to call the Close method of my main window from some VM.
 
Any idea around?
 
Thanks
 
Gerhard

Rishi Oberoi

unread,
Feb 23, 2011, 5:15:05 AM2/23/11
to nro...@googlegroups.com
If you need a programmatic way to know when an application is exiting, consider subscribing to the ApplicationStateInfo channel. 

Below is an example from the Square-Away game (see http://www.orktane.com/Blog/post/2010/07/19/Square-Away-A-Windows-Phone-7-Game-using-nRoute.aspx) where I use the knowledge of when the application is closing to only save the highest score just once in the application's lifetime:
    [MapService(typeof(IHighScoreService), Lifetime=InstanceLifetime.Singleton)]
    public class HighScoreService 
        : IHighScoreService
    {
        private const string HIGH_SCORE_KEY = "SQUAREAWAY_HIGH_SCORE_KEY";

        private int? _highestRecordedScore;
        private IDisposable _subscriptionToken;

        [ResolveConstructor]
        public HighScoreService(IChannel<ApplicationStateInfo> stateInfoChannel)
        {
            if (stateInfoChannel == nullthrow new ArgumentNullException("stateInfoChannel");
            _subscriptionToken = stateInfoChannel.Subscribe(OnApplicationStateChanged, null, ThreadOption.UIThread, false);

            if (IsolatedStorageSettings.ApplicationSettings.Contains(HIGH_SCORE_KEY))
            {
                _highestRecordedScore = Convert.ToInt32(IsolatedStorageSettings.ApplicationSettings[HIGH_SCORE_KEY]);
            }            
        }

#region Channel Handler

        private void OnApplicationStateChanged(ApplicationStateInfo stateInfo)
        {
            if (stateInfo.CurrentState == ApplicationState.Exiting)
            {
                IsolatedStorageSettings.ApplicationSettings.Save();
                _subscriptionToken.Dispose();
            }
        }

#endregion

#region IHighScoreService Implementation

        public void RegisterLastScore(int score)
        {
            if (score > _highestRecordedScore.GetValueOrDefault(-1))
            {
                IsolatedStorageSettings.ApplicationSettings[HIGH_SCORE_KEY] = score;                
                _highestRecordedScore = score;
            }
        }

        public int? GetHighestScore()
        {
            return _highestRecordedScore;
        }

#endregion

    }
Note how we check the current state of the application using "stateInfo.CurrentState" property - that means you can use this channel with the application's other lifetime events like starting, started etc.

Cheers,
Rishi

Gerhard Kreuzer

unread,
Feb 23, 2011, 5:22:06 AM2/23/11
to nro...@googlegroups.com
Hi Rishi,
 
thanks, will go into it, but I want to stop the app programmatically. I want not to know, when somebody press some exit button, I want 'press' this button programmatic, because some user forgot to log out and this is a security issue.
 
With best regards
 
Gerhard


Von: nro...@googlegroups.com [mailto:nro...@googlegroups.com] Im Auftrag von Rishi Oberoi
Gesendet: Mittwoch, 23. Februar 2011 11:15
An: nro...@googlegroups.com
Betreff: [nRoute] Re: Exit an app

Rishi Oberoi

unread,
Feb 23, 2011, 5:26:23 AM2/23/11
to nro...@googlegroups.com

Gerhard Kreuzer

unread,
Feb 23, 2011, 5:39:31 AM2/23/11
to nro...@googlegroups.com
Thanks, I search in help amd msdn .....


Von: nro...@googlegroups.com [mailto:nro...@googlegroups.com] Im Auftrag von Rishi Oberoi
Gesendet: Mittwoch, 23. Februar 2011 11:26
An: nro...@googlegroups.com
Betreff: Re: AW: [nRoute] Re: Exit an app

Reply all
Reply to author
Forward
0 new messages