从静态方法刷新/重画 [英] Refresh/Repaint from static method

查看:74
本文介绍了从静态方法刷新/重画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个自定义控件,该控件使控件在两种颜色之间闪烁.
我的问题是我不知道如何从静态方法执行Refresh()命令.
我需要从该静态方法更新所有自定义控件.

我试图覆盖Refresh().

 公共 覆盖  void 刷新()
{
    基本 .Refresh();
} 



但是如何从静态方法调用Refresh()函数?

谢谢.

解决方案

您将必须传递到该静态方法,或在类中静态注册要刷新的控件或获取它们的某种方式. .例如,您可以传递一个表单并刷新表单上该类型的所有成员:

public static RefreshAll<T>(Control cc){
 foreach(Control c in cc.Controls){
  if(c is T) c.Refresh();
  RefreshAll<T>(c);
 }
}



叫做

void Timer_Tick(object sender, TimerEventArgs e){
 RefreshAll<MyFlashyControl>(this);
}



...的形式.您可以将静态方法放在任何有意义的地方.


我的水晶球"是模糊的,但是...我猜想静态方法"需要参考自定义控件的实例(假设它们是用户控件),因为它自定义以来不能是静态的".

另一个猜测可能是静态方法在一个静态类中,该类也许是一个工厂",它按需返回自定义控件的实例,然后将这些实例定位"在某个运行时容器中.

如果您要谈论在运行时添加自定义控件的实例:

1.定义类型为"YourCustomControl"的静态集合,然后在创建自定义控件的代码中的位置将其添加到该集合中.然后,当然,您可以使该集合可供可以枚举它的静态方法访问,并在每个自定义控件上调用Refresh()方法.
需要考虑的事情:如果静态方法未在创建自定义控件的对象中定义":如何使它可被实例访问...例如,表单...新自定义控件所在的位置创建了吗?

如果您要谈论在设计时添加自定义控件的实例:

假设您将自定义控件拖放到某个容器对象(窗体,面板等)中,那么您将需要在自定义控件代码中执行某些操作,以正确地将新实例添加到静态列表中实例.

是否有一个适用于设计时和运行时的快乐解决方案"?嗯,我想是这样的:)

1.因为您可能在多个窗体上或在另一个窗体(嵌套)中的一个窗体或其他容器中拥有自定义控件的实例,所以我的选择是制作一个静态类,该类封装您需要对所有实例进行的操作自定义控件随处可见.

 公共 静态 公共 静态列表< YourCustomControl> YCCList = 列表< YourCustomControl>();

    公共 静态  void  RefreshYCCs()
    {
         foreach (您的ycc  in  YCCList中的)
        {
            ycc.Refresh();
        }
    }
} 

2.现在,您可以利用自定义控件的Load事件来更新将在设计时或在运行时创建实例的List:

 无效 YourCustomControl_Load(// 眼见为实... ... 
    //  Console.WriteLine(添加了自定义控件"); 
    //  
    TheStaticClass.YCCList.Add();
    //  
    // 建议您在此处设置一个断点并检查"YCCList的计数" 
    // 既可以在开始时添加,也可以在运行时添加自定义控件的实例
    //  
} 

3.现在,您可以从代码中的其他位置(在某些其他Form或其他任何内部)调用自定义控件的所有实例的刷新:

  private  无效 button1_Click(对象发​​送者,EventArgs e)
{
    TheStaticClass.RefreshYCCs();
} 

4.在这种情况下,我个人的偏好是将在运行时创建自定义控件新实例的代码(如果有的话)移到静态类中(即,使其成为工厂"). '类),然后当我在静态类中调用该方法时,我会将新实例要插入的容器传递给它,但是那是一种品味或风格. "cs"> 公共 静态 无效 AddNewCustomControl (控制targetControl) { targetControl.Controls.Add( YourCustomControl()); }

请注意,在概述此小片段时,我没有考虑到可能存在某些类型的容器对象,您可能希望在其中插入自定义控件的实例,而该实例可能无法在其内部访问ControlsCollection. 通常"的方式.偏执狂是害虫防治的朋友吗?



这里还暗含了其他策略,我相信您会在此线程上听到这些策略的一些其他愿景",以及我从未梦想过的其他愿景:)


Hi,

I got a Custom Control that is making the control flashing between two colors.
My problem is that I dont know how to do a Refresh() command from a static method.
I need to update all the custom controls from that static method.

I have tried to override Refresh().

public override void Refresh()
{
    base.Refresh();
}



But how do I call the Refresh() function from the static method?

Thanks.

解决方案

You would have to pass into that static method, or register statically in the class, the controls that you want to refresh or some way of getting to them. For example you could pass in a form and refresh all members of that type on the form:

public static RefreshAll<T>(Control cc){
 foreach(Control c in cc.Controls){
  if(c is T) c.Refresh();
  RefreshAll<T>(c);
 }
}



called like

void Timer_Tick(object sender, TimerEventArgs e){
 RefreshAll<MyFlashyControl>(this);
}



... in the form. You can put that static method anywhere that makes sense.


My "crystal ball" is cloudy, but ... I''m going to guess that the "static method" needs a reference to the instances of the custom controls (going to assume they are User Controls) which it doesn''t have now ... since a Control, by definition, cannot be ''static.''

Another guess might be the static method is in a static class that, perhaps, is a ''factory'' that returns instances of your custom control on-demand that are then "sited" in some run-time container.

If you are speaking about adding instances of your custom control at run-time:

1. define a static collection of type ''YourCustomControl,'' and then, at the place in the code where your custom control is created, add it to that collection. Then, of course, you make that collection accessible to the static method that can enumerate it, and invoke the Refresh() method on each custom control.

Something to think about: if the static method is not "defined in" the object where the custom controls are created: how do you make it accessible to the instance ... for example, a Form ... where the new custom controls are created ?

If you are speaking about adding instances of your custom control at design-time:

Assuming you are drag-dropping your custom control into some container object (Form, Panel, etc.), then you are going to need to do something in the custom control code that does the right thing to add the new instance to the static list of instances.

Is there a ''happy solution'' which will work for both design-time and run-time ? Well, I think so :)

1. Because you may have instances of your custom controls on more than one Form, or, in one Form or other container inside another (nested), my choice would be to make a static class that encapsulates what you need to do to all instances of the custom controls everywhere.

public static class TheStaticClass
{
    public static List<YourCustomControl> YCCList = new List<YourCustomControl>();

    public static void RefreshYCCs()
    {
        foreach (YourCustomControl ycc in YCCList)
        {
            ycc.Refresh();
        }
    }
}

2. Now you can take advantage of the Load event of your custom control to update the List which will work at design-time, or when you create instances at run-time:

private void YourCustomControl_Load(object sender, EventArgs e)
{
    // seeing is believing ... ?
    //Console.WriteLine("custom control added");
    //
    TheStaticClass.YCCList.Add(this);
    //
    // suggest you set a break-point here and examine the 'Count of the YCCList
    // both initially, and as you may add instances of your custom control at run-time
    //
}

3. And now from somewhere else (inside some other Form, or whatever) in your code you can invoke the refresh of all instances of your custom control:

private void button1_Click(object sender, EventArgs e)
{
    TheStaticClass.RefreshYCCs();
}

4. In this scenario my personal preference would be to move the code (if any) that creates a new instance of the custom control at run-time into the static class (i.e., make it a ''factory'' class), and when I called the method in the static class, I''d pass it the container the new instance is to be inserted into ... but that''s a matter of taste, or style.

public static void AddNewCustomControl(Control targetControl)
{
    targetControl.Controls.Add(new YourCustomControl ());
}

Note that in outlining this little snippet I did not consider the possibility there might be some type of container object into which you might wish to insert an instance of your custom control that just might not have its internal ControlsCollection accessible in the ''usual'' way. Paranoia is the friend of pest-control ?

~

There are other strategies implied here, and I''m sure you''ll hear some ''other visions'' of those strategies, and others that I never dreamed of, on this thread :)


这篇关于从静态方法刷新/重画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆