标签不会在if语句中触发

时间:2013-12-01 20:51:08

标签: c# wpf label alarm

嗨,这个WPF闹钟应用程序不会触发通过动画输入的标签。最初是以表格形式制作然后我带入WPF。在C#Form中,这一切都很好,一切都被触发了,但是只要我把它带到WPF就不会触发它。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Speech;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Dynamic;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Web;
using System.Timers;
using System.Runtime.InteropServices;
using System.Windows.Threading;
using System.Diagnostics;
using System.Windows.Media.Animation;

namespace Alarm_Clock_WPF
{
    public partial class MainWindow : Window
    {
        DispatcherTimer dispatcherTimer1 = new DispatcherTimer();
        DispatcherTimer dispatcherTimer2 = new DispatcherTimer();
        public MainWindow()
        {
            System.Windows.Threading.DispatcherTimer dispathcherTimer = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer1.Tick += new EventHandler(dispatcherTimer1_Tick);
            dispatcherTimer2.Tick += new EventHandler(dispatcherTimer2_Tick);
            dispatcherTimer1.Interval = new TimeSpan(0, 0, 1);
            dispatcherTimer1.Start();
            dispatcherTimer2.Start();
            InitializeComponent();
        }

        private void dispatcherTimer1_Tick(object sender, EventArgs e)
        {
            CountTime.Content = DateTime.Now.Hour.ToString("00") + ":" + DateTime.Now.Minute.ToString("00") + ":" + DateTime.Now.Second.ToString("00");

            if (CountTime.Content == TriggerTime.Content)
            {
                DateTime now2 = DateTime.Now;
                string date1 = DateTime.Today.ToString("D");
                string time2 = now2.GetDateTimeFormats('t')[0];
                //Label Leave Animation
                //DoubleAnimation animation = new DoubleAnimation(0, TimeSpan.FromSeconds(2));
                //label4.BeginAnimation(OpacityProperty, animation);
                System.Diagnostics.Process.Start("https://www.youtube.com/");
                //Music
                System.Media.SoundPlayer sp = new System.Media.SoundPlayer(@"C:\Users\*****\Documents\Wake up theme 01.wav");
                sp.Play();
                //Video
                //Video beach = new Video(@"C:\Users\******\Desktop\Jarvis\...avi");
                //beach.Play();
                label1.Content = "Done";
                //Actual Wake Up Call
                dispatcherTimer1.Stop();
            }
        }

        private void dispatcherTimer2_Tick(object sender, EventArgs e)
        {
            //Alarm settings
            if (CountTime.Content == TriggerTime.Content)
            {
                DateTime now2 = DateTime.Now;
                string date1 = DateTime.Today.ToString("D");
                string time2 = now2.GetDateTimeFormats('t')[0];
                //Label Leave Animation
                //DoubleAnimation animation = new DoubleAnimation(0, TimeSpan.FromSeconds(2));
                //label4.BeginAnimation(OpacityProperty, animation);
                System.Diagnostics.Process.Start("https://www.youtube.com/");
                //Music
                System.Media.SoundPlayer sp = new System.Media.SoundPlayer(@"C:\Users\*******\Documents\Wake up theme 01.wav");
                sp.Play();
                //Video
                //Video beach = new Video(@"C:\Users\*******\Desktop\Jarvis\...avi");
                //beach.Play();
                label1.Content = "Done";
                //Actual Wake Up Call
                dispatcherTimer1.Stop();
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            TriggerTime.Content = DateTime.Today.Hour.ToString("00") + ":" + DateTime.Today.Minute.ToString("00") + ":" + DateTime.Today.Second.ToString("00");
        }
    }
}

显然编码本身非常糟糕,但我在这里的原因是找出为什么2个标签“CountTime和TriggerTime”不会触发,如果我设置它们!=它们会自动触发。但是当设置==他们没有。

1 个答案:

答案 0 :(得分:1)

总结评论。由于ContentObject,因此您可以通过引用对其进行比较。使用被Equals覆盖的String方法来比较值:

CountTime.Content.Equals(TriggerTime.Content)

或将这两个值投放到String然后进行比较:

(string)CountTime.Content == (string)TriggerTime.Content