2016-06-15 139 views
0

我的代码启动DispatcherTimer。检测用户活动windows phone 8

我只需要检测任何种类的用户联系,以重新启动DispatcherTimer?

public partial class MainMenu : PhoneApplicationPage 
{ 
    public MainMenu() 
    { 
     InitializeComponent(); 
     startTimer(); 
    } 

    private DispatcherTimer dispatcherTimer; 
    private void startTimer() 
    { 
     dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); 
     dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); 
     dispatcherTimer.Interval = new TimeSpan(0, 0, 1); 
     dispatcherTimer.Start(); 
    } 
    private void dispatcherTimer_Tick(object sender, EventArgs e) 
    { 
     dispatcherTimer.Stop(); 
     AllMyFunctions.logout_code(this); 
    } 

    private void restartTimer() 
    { 
     dispatcherTimer.Stop(); 
     startTimer(); 
    } 
} 

如何检测任何种类的用户交互以便触发restartTimer()方法?

回答

0

我在每个PhoneApplicationPage的Tap上添加了一个事件。

int timeOutInSeconds; 
Timer timerObject; 

private void onPageLoad() 
{ 
    timeOutInSeconds = 30; 
    timerObject = new Timer(); 
    timerObject.startTimer(this, timeOutInSeconds, appsettings); 
} 
private void LayoutRoot_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
{ 
    timerObject.restartTimer(timeOutInSeconds); 
} 

public class Timer 
{ 
    public DispatcherTimer dispatcherTimer; 
    public PhoneApplicationPage phoneApplicationPage; 
    public IsolatedStorageSettings appsettings; 


    //public Timer(PhoneApplicationPage callingPhoneApplicationPage) 
    //{ 
    // this.phoneApplicationPage = callingPhoneApplicationPage; 
    //} 

    public void startTimer(PhoneApplicationPage callingPhoneApplicationPage, int noOfSeconds, IsolatedStorageSettings calledAppsettings) 
    { 
     this.appsettings = calledAppsettings; 
     this.phoneApplicationPage = callingPhoneApplicationPage; 
     dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); 
     dispatcherTimer.Tick += new EventHandler(timer_time_done); 
     dispatcherTimer.Interval = new TimeSpan(0, 0, noOfSeconds); 
     dispatcherTimer.Start(); 
    } 
    public void timer_time_done(object sender, EventArgs e) 
    { 
     dispatcherTimer.Stop(); 
     BsgFunctions.logout_Forcefully_code(phoneApplicationPage, this, appsettings); 
    } 

    public void restartTimer(int noOfSeconds) 
    { 
     dispatcherTimer.Stop(); 
     startTimer(phoneApplicationPage, noOfSeconds,appsettings); 
    } 

    public void stopTimer() 
    { 
     dispatcherTimer.Stop(); 
    } 

}