1. 程式人生 > >WPF Timer控制窗體顏色漸變

WPF Timer控制窗體顏色漸變

簡介:

 本文主要寫到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);
            }
        }
    }
}

截圖: