定时器:
每个指定时间执行一次,在Winform中实现定时器采用的是system.Windows.Forms.timer命名空间下的定时器,
但是在WPF下使用定时器是Dispatcher类实现定时器,该定时器是在system.Windows.threading下
创建定时器
DispatcherTimer timer = new DispatcherTimer();
设置定时器时间间隔 TimeSpan.FormSeconds(1) 以秒为单位
timer1.Interval = TimeSpan.FromSeconds(1);
举个例子,就如:
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.Shapes;
using System.Windows.Threading;
namespace WpfProgram.Controls
{
/// <summary>
/// Windowlabel.xaml 的交互逻辑
/// </summary>
public partial class Windowlabel : Window
{
public DispatcherTimer timer1 = new DispatcherTimer();
public Windowlabel()
{
InitializeComponent();
// 定时器:每个指定时间执行一次,在Winform中实现定时器采用的是system.Windows.Forms.timer命名空间下的定时器,但是在WPF下使用定时器是Dispatcher类实现定时器,该定时器是在system.Windows.threading下
// 创建定时器
// DispatcherTimer timer = new DispatcherTimer();
// 设置定时器时间间隔 TimeSpan.FormSeconds(1) 以秒为单位
timer1.Interval = TimeSpan.FromSeconds(1);
// 设置每隔时间要执行的方法,绑定方法到定时器上
// Tick是事件名
timer1.Tick += new System.EventHandler(F1);
// 开启定时器
// timer.Start();
// 关闭定时器
// timer.Stop();
}
// 定义随即对象
public Random r = new Random();
// 定时器的事件函数
public void F1(object sender,EventArgs e)
{
Console.WriteLine("李克兢最帅");
int r1 = r.Next(0, 255);
int r2 = r.Next(0, 255);
int r3 = r.Next(0, 255);
this.l1.Background = new SolidColorBrush(Color.FromRgb((byte)r1, (byte)r2, (byte)r3));
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// 修改label的背景颜色
// 1.Brushes.Red
// 2.使用三原色
// SolidColorBrush 创建一个笔刷 颜色是参数
this.l1.Background = new SolidColorBrush(Color.FromRgb(255, 250, 0));
// 点击时使用定时器切换颜色
timer1.Start();
}
}
}
Label:
HoriaontalContentAilgnment="Center" 文本内容水平居中
VerticalContentAlignment="Center" 文本内容竖直居中
TextBox:
FontFamily: 字体样式 例如:宋体或者楷体
FontWeight: 字体粗细 Bold加粗
FontStyle: 斜体和正常体
BorderBrush: 边框颜色
BorderThickness: 边框大小
SelectionBrush: 选中字体背景颜色
TextChanged: 当输入内容变化的触发事件