可以做一个单例类 public class ControlService { private static ControlService service = null; public ControlService GetInstance() { if( null == service ) { service = new ControlService(); } return service; } public event EventHandler notifyHandler;public void CallNotifyHandler(object sender, EventArgs e) { EventHandler tmp = notifyHandler; if (null != tmp) { tmp(sender, e); } } }每个窗体都向这个单例类注册事件,属性改变的时候调用CallNotifyHandler来通知每个窗体来更改属性
可以用静态成员做的。但是不用事件通知。 你把所有的实例都放到一个List里面存起来就可以了。不过这样做有个缺陷, 就是所有控件的实例不会被GC回收了。 因为永远有一个静态类抓着它。 如果要再解决GC的问题, 就要再复杂一点。 这里只是提供个思路。示例代码如下: namespace ConsoleApplication11 { class Program { static void Main(string[] args) { MyControl control = new MyControl(); control = new MyControl(); control = new MyControl(); control = new MyControl(); control = new MyControl(); control = new MyControl(); MyControl.Skin = "test"; } } public class MyControl { private static string _skin = string.Empty; public static List<MyControl> _instances = new List<MyControl>(); public MyControl() { _instances.Add(this); } public static string Skin { get { return _skin; } set { _skin = value; foreach (MyControl c in _instances) { // 你的更新逻辑 c.Refresh(); } } } public void Refresh() { Console.WriteLine(this.GetHashCode() + "已经被更新!"); } } }
修改XML文件
public class ControlService
{
private static ControlService service = null;
public ControlService GetInstance()
{
if( null == service )
{
service = new ControlService();
}
return service;
} public event EventHandler notifyHandler;public void CallNotifyHandler(object sender, EventArgs e)
{
EventHandler tmp = notifyHandler;
if (null != tmp)
{
tmp(sender, e);
}
}
}每个窗体都向这个单例类注册事件,属性改变的时候调用CallNotifyHandler来通知每个窗体来更改属性
你把所有的实例都放到一个List里面存起来就可以了。不过这样做有个缺陷, 就是所有控件的实例不会被GC回收了。
因为永远有一个静态类抓着它。
如果要再解决GC的问题, 就要再复杂一点。
这里只是提供个思路。示例代码如下:
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
MyControl control = new MyControl();
control = new MyControl();
control = new MyControl();
control = new MyControl();
control = new MyControl();
control = new MyControl(); MyControl.Skin = "test";
} }
public class MyControl
{
private static string _skin = string.Empty;
public static List<MyControl> _instances = new List<MyControl>(); public MyControl()
{
_instances.Add(this);
} public static string Skin
{
get
{
return _skin;
}
set
{
_skin = value;
foreach (MyControl c in _instances)
{
// 你的更新逻辑
c.Refresh();
}
}
} public void Refresh()
{
Console.WriteLine(this.GetHashCode() + "已经被更新!");
}
}
}
要小心处理了。
因为析构在remove之后。那就放到Dispose里面好了。或者用的不多的话不管也没多大事。只能连续回复三次。
public partial class CustomControl1 : Control
{
public CustomControl1()
{
InitializeComponent();
ShowMsg += new OneStr(CustomControl1_ChangeSkin);
}
void CustomControl1_ChangeSkin(string msg)
{
myText = msg;
this.Invalidate();
}
private string myText="默认的"; public delegate void OneStr(string msg);
public static event OneStr ShowMsg; /// <summary>
/// 这个方法的调用影响所有实例
/// </summary>
/// <param name="msg"></param>
public static void RaiseChange(string msg)
{
ShowMsg(msg);
} protected override void OnPaint(PaintEventArgs pe)
{
// TODO: 在此处添加自定义绘制代码 // 调用基类 OnPaint
base.OnPaint(pe);
pe.Graphics.DrawString(myText, Font, Brushes.Red, PointF.Empty);
}
}
所以还是要静态属性,将静态方法RaiseChange在set时调用。