Windows Phone - 应用程序级别DispatcherTimer

时间:2017-02-04 06:58:17

标签: c# session windows-phone-8 timer dispatchertimer

我的要求:每个页面都需要一个会话计时器(登录页面除外)。如果计时器超过60秒且未执行任何用户操作,则应重定向到登录页面。

我已成功完成上述一个页面的要求,其中包含以下代码段。

        DispatcherTimer dispatcherTimer;
        DateTimeOffset startTime;
        DateTimeOffset lastTime;
        DateTimeOffset stopTime;
        int timesTicked = 1;
        int timesToTick = 60; 


        page_load()
        {  
           dispatcherTimer = new DispatcherTimer();
           dispatcherTimer.Tick += dispatcherTimer_Tick;
           dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
           startTime = DateTimeOffset.Now;
           lastTime = startTime;
           dispatcherTimer.Start();    
        } 


        private void PhoneApplicationPage_ManipulationCompleted(object sender, System.Windows.Input.ManipulationCompletedEventArgs e)
        {
            startTime = DateTimeOffset.Now;
            lastTime = startTime;
            dispatcherTimer.Start();    
        }

       void dispatcherTimer_Tick(object sender, object e)
       {
           DateTimeOffset time = DateTimeOffset.Now;
           TimeSpan span = time - lastTime;
           lastTime = time;
           timesTicked++;
           if (timesTicked > timesToTick)
           {
               isTimeOver = true;
               stopTime = time;

               dispatcherTimer.Stop();

               span = stopTime - startTime;
               MessageBox.Show("Login again", "Session Expired", MessageBoxButton.OK);
               NavigationService.Navigate(new Uri("/Login_3.xaml", UriKind.Relative)); 
           }
       }

每页都需要这个逻辑。由于在导航的自定义类中无法访问页面,我感到困惑。 我不认为在每个页面中撰写dispatcherTimer_Tick事件都是很好的方法。 你能不能请任何人建议我们如何在一个可用于每个页面的代码中做到这一点? 在此先感谢:)

1 个答案:

答案 0 :(得分:0)

您可以为每个页面创建一个公共基类来继承并在那里编写此代码。

示例:

namespace Test
{     
    public class PageBaseWithTimer: PhoneApplicationPage
    {
        DispatcherTimer dispatcherTimer;

        ... //Copied from question

        int timesToTick = 60; 

        PageBase()
        {
            this.Loaded += PageBase_Loaded;
            this.OnManipulationCompleted += PhoneApplicationPage_ManipulationCompleted
        }

        private void PageBase_Loaded(object sender, RoutedEventArgs e)
        {
             dispatcherTimer = new DispatcherTimer();

             ... //Copied from question

             dispatcherTimer.Start();
        }

        private void PhoneApplicationPage_ManipulationCompleted(object sender, System.Windows.Input.ManipulationCompletedEventArgs e)
        {
            startTime = DateTimeOffset.Now;
            lastTime = startTime;
            dispatcherTimer.Start();  
        }      

        void dispatcherTimer_Tick(object sender, object e)
        {
            DateTimeOffset time = DateTimeOffset.Now;

            ... //Copied from question

            MessageBox.Show("Login again", "Session Expired", MessageBoxButton.OK);
            NavigationService.Navigate(new Uri("/Login_3.xaml", UriKind.Relative)); 
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs args)
        {
            //dispose timer
            dispatcherTimer.Stop();
            dispatcherTime.Tick -= dispatcherTimer_Tick
            dispatcherTimer = null;
        }
    }
}

在需要此计时器的页面中继承上述类。如果不需要,请记住在基类中处理计时器,同时导航。

    public partial class PageWithTimer1 : PageBaseWithTimer
    { 
        public PageWithTimer1()
        {
            InitializeComponent();
        }           
    }

   <c:PageBaseWithTimer xmlns:c="clr-namespace:Test"
        x:Class="BBM.UI.Views.PageWithTimer1" 
        ...

   </c:PageBaseWithTimer>

确保为基类public提供PageBaseWithTimer的访问修饰符,以使其可用于所有页面。