Example code for inactivity timeout in Silverlight 4 application (using MVVM)
I'm building my first SL Out of Browser app. After say 20 minutes of inactivity, I want to set my App.IsAuthenticated = false, and redirect to the logon page.
I've googled the heck out of this, I've read tons of discussions that talk about using mousemove/keydown event handlers with a dispatch timer, but I haven't seen a lick of code anywhere showing how this is done.
I'm using MVVM if that makes a difference (e.g. code would go in my MainViewModel, so I'd like example code to fit that pattern).
Can anyone provide sample code for this? It's going to be simple I'm sure开发者_如何学编程, but I've only been coding in .NET for the last month or so, and most of it has been SL.
Thanks, Scott
The 5 second timer for demonstration:
public partial class MainPage : UserControl
{
private DispatcherTimer timer;
public MainPage()
{
InitializeComponent();
timer = new DispatcherTimer(){Interval = TimeSpan.FromSeconds(5)};
timer.Tick += (s, e) => { this.textBlock.Text = "Time out"; this.timer.Stop(); };
timer.Start();
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
timer.Start();
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
timer.Start();
}
}
精彩评论