Hi this WPF alarm app wont trigger the label to be fed in via animation. Initially it was made in a Form then I brought into WPF. When in C# Form this was all fine, everything triggered, but as soon as I brought it into WPF it wouldn't trigger.

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");
        }
    }
}

Obviously the coding itself is pretty bad, but the reason im here is to find out why the the 2 labels "CountTime and TriggerTime" wont trigger, if I set them != they trigger automatically. But when set == they dont.

有帮助吗?

解决方案

To sum up comments. Because Content is an Object you compare it by reference. Use Equals method, which is overridden by String, to compare values:

CountTime.Content.Equals(TriggerTime.Content)

or cast both values to String and then compare them:

(string)CountTime.Content == (string)TriggerTime.Content
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top