image.png
代码:
MainWindow.xaml
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="XX抽奖系统" Height="300" Width="500" WindowStartupLocation="CenterScreen" ResizeMode="NoResize">
<Canvas>
<Label Name="lblName" Content="XXXXXXX" Canvas.Left="101" Canvas.Top="59" Width="267" Height="69" FontSize="50" RenderTransformOrigin="0.266,0.203"/>
<Button Content="开始" Name="btnStart" Width="150px" FontSize="30px" Height="50px" Canvas.Left="52" Canvas.Top="189" Click="BtnStart_Click"/>
<Button Content="停止" Name="btnEnd" Width="150px" FontSize="30px" Height="50px" Canvas.Left="233" Canvas.Top="189" Click="BtnEnd_Click"/>
</Canvas>
</Window>
MainWindow.xaml.cs
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 WpfApp1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
}
//定义存放姓名的集合
List<string> list = new List<string>();
DispatcherTimer time;
Random random = new Random();
/// <summary>
/// 初始化事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Class1 class1 = new Class1();
Console.WriteLine(class1.FindUser("zhangsan"));
list.Add("张三");
list.Add("李四");
list.Add("王五");
list.Add("赵六");
}
/// <summary>
/// 开始事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnStart_Click(object sender, RoutedEventArgs e)
{
time = new DispatcherTimer();
time.Interval = TimeSpan.FromMilliseconds(10);
time.Tick += Time_Tick;
time.Start();
}
private void Time_Tick(object sender, EventArgs e)
{
//判断计时器是否正在运行
if (time.IsEnabled==true)
{
int num = random.Next(0,4);
lblName.Content= list[num];
}
}
/// <summary>
/// 停止事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnEnd_Click(object sender, RoutedEventArgs e)
{
// time.IsEnabled = false;
lblName.Content = list[0];
time.Stop();
}
}
}