如何将CountDown添加到此应用程序?

时间:2014-05-06 22:54:50

标签: c# wpf

我有一个wpf应用程序,每隔一段时间最大化一个窗口,我想在这个窗口中添加一个基于Quartz.net作业运行的秒数倒计数到零的文本块

<Window x:Class="EyeCare2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        Background="Orange"
        KeyDown="Window_KeyDown" Closing="Window_Closing" >
    <Grid>

    </Grid>
</Window>

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Init();
        }


        private void Init()
        {
            ISchedulerFactory schedFact = new StdSchedulerFactory();
            IScheduler sched = schedFact.GetScheduler();

            IDictionary<string, object> map = new Dictionary<string, object>();
            map.Add("window", this);

            IJobDetail job = JobBuilder.Create<ShowJob>()
             .WithIdentity("showJob", "group")
             .UsingJobData(new JobDataMap(map))
             .Build();

            ITrigger trigger = TriggerBuilder.Create()
              .WithIdentity("showTrigger", "group")
              .StartNow()
              .WithSimpleSchedule(x => x.WithIntervalInMinutes(20).RepeatForever())
              .Build();

            sched.ScheduleJob(job, trigger);
            sched.Start();
          //  sched.StartDelayed(TimeSpan.FromSeconds(5));
            //sched.StartDelayed(TimeSpan.FromMinutes(20));
            this.Hide();
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Pause)
            {
                this.Hide();
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //Do some stuff here 

            //Hide Window
            Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, (DispatcherOperationCallback)delegate(object o)
            {
                Hide();
                return null;
            }, null);

            //Do not close application
            e.Cancel = true; 
        }
    }

   public class ShowJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Window win = context.JobDetail.JobDataMap.Get("window") as Window;

            if (win != null)
            {
                Application.Current.Dispatcher.Invoke((Action)(() =>
                {
                    win.Width = System.Windows.SystemParameters.FullPrimaryScreenHeight;
                    win.Height = System.Windows.SystemParameters.FullPrimaryScreenHeight;
                    win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                    win.WindowStyle = WindowStyle.SingleBorderWindow;
                    win.Topmost = true;
                    win.WindowState = WindowState.Maximized;
                    win.Show();
                }));

                IDictionary<string, object> map = new Dictionary<string, object>();
                map.Add("window", win);

                IJobDetail job = JobBuilder.Create<HideJob>()
                 .WithIdentity("hideJob", "group")
                 .UsingJobData(new JobDataMap(map))
                 .Build();

                ITrigger trigger = TriggerBuilder.Create()
                  .WithIdentity("hideTrigger", "group")
                  .StartAt(DateBuilder.FutureDate(40, IntervalUnit.Second))
                  .Build();

                context.Scheduler.ScheduleJob(job, trigger);
            }
        }
    }
public class HideJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        Window win = context.JobDetail.JobDataMap.Get("window") as Window;
        if (win != null && Application.Current != null)
        {
            Application.Current.Dispatcher.Invoke((Action)(() =>
            {
                win.Hide();
            }));
        }
    }
}

我想也许我可以添加一个文本块,然后使用调度计时器来实现这一点,但不知道如何连接它,我认为它可能必须在ShowJob类的某个地方,但是distpatch计时器没有运行所以我猜它是不是在正确的线程上。

0 个答案:

没有答案