Timer for traffic lights c#

2020-05-09 22:49发布

问题:

i have wrote the code for a simple traffic light app with wpf elements. I use the timer function. The app works without any exception, but the wanted function isnt there. It only flashes the red or the orange light. I sit for 3 hours and now brain is empty. I try and try without any change. Can you help me please to fix the problem and learn how to use the timer correct? Thanks a lot

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Windows.Threading;

namespace WPF_Timer
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        DispatcherTimer TimerRed, TimerRedOrange, TimerGreen, TimerOrange;
        public MainWindow()
        {
            InitializeComponent();

            TimerRed = new DispatcherTimer();
            TimerRed.Interval = new TimeSpan(0, 0, 10);
            TimerRedOrange = new DispatcherTimer();
            TimerRedOrange.Interval = new TimeSpan(0, 0, 5);
            TimerGreen = new DispatcherTimer();
            TimerGreen.Interval = new TimeSpan(0, 0, 10);
            TimerOrange = new DispatcherTimer();
            TimerOrange.Interval = new TimeSpan(0, 0, 2);
            TimerRed.Tick += TimerRed_Tick;
            TimerRedOrange.Tick += TimerRedOrange_Tick;
            TimerGreen.Tick += TimerGreen_Tick;
            TimerOrange.Tick += TimerOrange_Tick;
        }

        private void TimerRed_Tick(object sender, EventArgs e)
        {
            Epsered.Fill = Brushes.Red;
            Epseorange.Fill = Brushes.DarkOrange;
            Epsegreen.Fill = Brushes.DarkGreen;

            TimerRed.Stopp();
            TimerRedOrange.Start();
            TimerGreen.Stop();
            TimerOrange.Stop();

        }

        private void TimerRedOrange_Tick(object sender, EventArgs e)
        {
            Epsered.Fill = Brushes.Red;
            Epseorange.Fill = Brushes.Orange;
            Epsegreen.Fill = Brushes.DarkGreen;


            TimerRed.Stop();
            TimerGreen.Start();
            TimerOrange.Stop();
            TimerRedOrange.Stop();

        }

        private void TimerGreen_Tick(object sender, EventArgs e)
        {
            Epsegreen.Fill = Brushes.Green;
            Epsered.Fill = Brushes.DarkRed;
            Epseorange.Fill = Brushes.DarkOrange;

            TimerRed.Stop();
            TimerRedOrange.Stop();
            TimerOrange.Start();
            TimerGreen.Stop();
        }

        private void TimerOrange_Tick(object sender, EventArgs e)
        {
            Epseorange.Fill = Brushes.Orange;
            Epsered.Fill = Brushes.DarkRed;
            Epsegreen.Fill = Brushes.DarkGreen;

            TimerRed.Start();
            TimerRedOrange.Stop();
            TimerGreen.Stop();
            TimerOrange.Stop();

        }

        private void BtnStop_Click(object sender, RoutedEventArgs e)
        {
            Epsered.Fill = Brushes.White;
            Epseorange.Fill = Brushes.White;
            Epsegreen.Fill = Brushes.White;

            TimerRed.Stop();
            TimerRedOrange.Stop();
            TimerGreen.Stop();
            TimerOrange.Stop();

        }

        private void BtnStart_Click(object sender, RoutedEventArgs e)
        {

            TimerRed.Start();
            TimerRedOrange.Stop();
            TimerGreen.Stop();
            TimerOrange.Stop();

        }
    }  
}

回答1:

I think you had a good start; just too many timers. I am trying to learn WPF and I thought this would be a quick project to practice on. Notice in the XAML, I started with the Red Light on. I changed the colors a bit to get more contrast (old eyes.) In the code I cut back to one timer and check the current colors with the if statements. Notice, I did preserve the shorter time for the Orange light.

<Window x:Class="StopLight.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:StopLight"
        mc:Ignorable="d"
        Title="MainWindow" Height="300" Width="200">
    <Grid>
        <Border BorderThickness ="5" Height="100" Width="50" BorderBrush="Black">
            <StackPanel>
                <Ellipse x:Name="RedLight" Fill="Red"  Height="30" Width="30" />
                <Ellipse x:Name="OrangeLight" Fill="PaleGoldenrod"  Height="30" Width="30"  />
                <Ellipse x:Name="GreenLight" Fill="PaleGreen"  Height="30" Width="30"  />
            </StackPanel>
        </Border>
    </Grid>
</Window>

And here is the C# code...

public partial class MainWindow : Window
    {
        DispatcherTimer disTime = new DispatcherTimer();
        public MainWindow()
        {
            InitializeComponent();
            disTime.Interval = new TimeSpan(0, 0, 10);
            disTime.Tick += disTime_Tick;
            disTime.Start();
        }
        private void disTime_Tick(object sender, EventArgs e)
        {
            if (RedLight.Fill == Brushes.Red)
            {
                RedLight.Fill = Brushes.PaleVioletRed;
                GreenLight.Fill = Brushes.Green;
            }
            else if (GreenLight.Fill == Brushes.Green)
            {
                GreenLight.Fill = Brushes.PaleGreen;
                OrangeLight.Fill = Brushes.Orange;
                disTime.Interval = new TimeSpan(0, 0, 5);
            }
            else if (OrangeLight.Fill ==Brushes.Orange)
            {
                OrangeLight.Fill = Brushes.PaleGoldenrod;
                RedLight.Fill = Brushes.Red;
                disTime.Interval = new TimeSpan(0, 0, 10);
            }
        }
    }