简介:
本文主要写到System.Windows.Forms.Timer的四个属性,利用这四个属性,去不断改变窗体的颜色
案例:
了解 Background 的值 “#1000” ,“#”后的第一位数字由小变大表示着本颜色由浅到深
引用:System.Windows.Forms.dll 下载:http://download.csdn.net/tag/Timer.dll
源码:
------------------View
<Windowx:Class="Demo_Mvvm.Views.WindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WindowView" Height="300" Width="300">
<Grid Background="{Binding Background}">
</Grid>
</Window>
----------------ViewModel
using System;
using System.Windows;
using System.Threading;
using System.Collections.ObjectModel;
using SimpleMvvmToolkit;
namespace Demo_Mvvm.ViewModels
{
public class WindowViewModel : ViewModelBase<WindowViewModel>
{
//引用Timer
System.Windows.Forms.Timer CustomerSystemTimer;
public WindowViewModel()
{
//实例Timer
CustomerSystemTimer = new System.Windows.Forms.Timer();
//设置时间
CustomerSystemTimer.Interval = 1000;
//事件的执行任务
CustomerSystemTimer.Tick += new EventHandler(ChangeTopic);
//开始执行
CustomerSystemTimer.Start();
}
public void ChangeTopic(objectsender, EventArgs e)
{
//TimerStop
CustomerSystemTimer.Stop();
BackgroundWay();
//TimerStart
CustomerSystemTimer.Start();
}
public void BackgroundWay()
{
if (Background != null)
{
Background = (Convert.ToInt32((Background.Split('0')[0].Trim().ToString()).Split('#')[1].Trim().ToString()) - 9 == 0) ?"#1090" : "" + Background.Replace((Background.Split('0')[0].Trim().ToString()).Split('#')[1].Trim().ToString(),"" + (Convert.ToInt32((Background.Split('0')[0].Trim().ToString()).Split('#')[1].Trim().ToString()) + 1).ToString() +"") + "";
}
}
private string _background;
public string Background
{
get
{
if (_background == null)
{
_background = "#1090";
}
return _background;
}
set
{
_background = value;
NotifyPropertyChanged(x => x.Background);
}
}
}
}
截图: