日期:2014-05-17  浏览次数:21015 次

全局变量的事件
1,要求定义一个全局变量,当变量发生变化时,能产生一个事件。
2,比如,在不同窗体内这变量发生变化,所有引用变量的窗体都能感知到.

------解决方案--------------------
参考http://www.cnblogs.com/h20064528/archive/2013/01/08/2850637.html
------解决方案--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    static class GlobalData
    {
        public static event Action DataChanged;
        static string _value = string.Empty;
        public static string Value
        {
            get
            {
                return _value;
            }
            set
            {
                if (DataChanged != null) DataChanged();
                _value = value;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            GlobalData.DataChanged += () => Console.WriteLine("更改!");
            GlobalData.Value = "a";
            GlobalData.Value = "b";
        }
    }
}