2016-06-15 73 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(); 
    } 

}