我用wpf元素编写了一个简单的交通灯应用程序的代码。 我使用计时器功能。 该应用程序无任何异常运行,但所需功能不存在。 它仅闪烁红色或橙色灯。 我坐了三个小时,现在脑子空了。 我尝试并尝试不做任何更改。 您能帮我解决问题并学习如何正确使用计时器吗? 非常感谢
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();
}
}
}
答案 0 :(得分:2)
我认为您有一个良好的开端;计时器太多了。我正在尝试学习WPF,我认为这将是一个快速的实践项目。注意,在XAML中,我从打开红灯开始。我稍微改变了颜色以获得更多的对比度(老眼睛)。 在代码中,我减少了一个计时器,并使用if语句检查当前颜色。请注意,我确实保留了更短的橙色灯时间。
<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>
这是C#代码...
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);
}
}
}