从类函数访问UI [英] Access UI from class functions

查看:86
本文介绍了从类函数访问UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在访问UI来提供一些分布在类内的数据时遇到问题.

我的程序实际上启用和禁用了el.其他PC进行某些关机/重启的功能(当然,在获得技术支持时,这有时是必需的,当然还有相关的硬件).

我已经意识到,在用于外部设备的一系列类中.
此类包含一个计时器,该计时器可提供定时的电源开/关命令.

Hi,

I have a problem accessing the UI to provide some data which are distributed inside a class.

My program actually enables and disables el. power of other pc''s to do some shutdown/restart (sometimes necessary when you''re in tech. support, with associated HW of course).

I have realized that in a array of classes used for the external devices.
This class contains a timer which gives timed power on/off orders.

namespace SW_Bootmachine
{
    public partial class Form1 : Form
    {
        public class Device
        {
            // other code...

            Timer t1 = new Timer();

            void t1_Tick(object sender, EventArgs e)
            {
                // power on device
                SusiIOWriteEx(byPinNr, true);     // enable ext. power
                t1.Interval = iOnTime * 1000;
                t1.Stop();
                t1.Start();
                bPowerON = true;

                // write current time into a label
            }
        }

        public Device[] Devices;



在点//将当前时间写入标签时,我想将设备1的当前时间写入Form1的名为 lblLastBootDev1 的标签中,以告诉用户上次启动系统的时间(其他7个设备也需要完成此操作)

通常,您只需命名即可访问程序代码中的标签,但是在我的类中这是不可能的.



At the point // write current time into a label I''d like to write the current time for Device 1 into a label called lblLastBootDev1 of Form1, to tell the user when the system was started the last time (this ofc also needs to be done for the other 7 devices)

Usually you can access the labels in your program code by just naming them, but inside my class this is not possible.

Could somebody tell me the best way to fill labels of Form1 with data of my class?

推荐答案

你好.

您可以使用事件处理程序来做到这一点.

首先创建一个类来存储要传输的事件数据:
Hello.

You can do this by using event handlers.

First create a class to store the event data you want to transfer:
public class TimerEventArgs : EventArgs
{
    public string TickTime;
}



然后在您的Device类中添加一个EventHandler:



Then in your Device class add a EventHandler:

public event EventHandler<timereventargs> OnTick;</timereventargs>



并使用它来通知更新:



And use it to notify an update:

if (OnTick != null)
{
    OnTick(this, new TimerEventArgs() {TickTime = DateTime.Now.ToLongTimeString()});
}



剩下要做的就是使用事件处理程序,因此在您创建Device实例时,在您的表单中为其分配一个处理程序:



All that''s left to do is to use the event handler, so in you form when you create your Device instance assign it an handler:

Devices[1].OnTick += new EventHandler<timereventargs>(Form1_OnTick);
</timereventargs>



当处理程序启动时,只需更新所需的标签即可:



And when the handler is fired up just update the label you want:

void Form1_OnTick(object sender, TimerEventArgs e)
{
    if (sender == Devices[0])
    {
        label1.Text = e.TickTime;
    }
    if (sender == Devices[1])
    {
        label2.Text = e.TickTime;
    }
}



这是完整的代码示例:



Here is a complete code sample:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        Devices = new Device[2];
        Devices[0] = new Device();
        Devices[1] = new Device();
        Devices[0].OnTick += Form1_OnTick;
        Devices[1].OnTick += Form1_OnTick;
    }

    void Form1_OnTick(object sender, TimerEventArgs e)
    {
        if (sender == Devices[0])
        {
            label1.Text = e.TickTime;
        }
        if (sender == Devices[1])
        {
            label2.Text = e.TickTime;
        }
    }

    public Device[] Devices;
}





public class Device
{
    public Device()
    {
        t1 = new Timer();
        t1.Interval = new Random().Next(100, 200);
        t1.Tick += new EventHandler(t1_Tick);
        t1.Start();
    }

    private Timer t1;
    private int iOnTime = 0;
    public event EventHandler<TimerEventArgs> OnTick;

    void t1_Tick (object sender, EventArgs e)
    {
        t1.Interval = new Random().Next(10, 20) * 100;
        t1.Stop();
        t1.Start();
        if (OnTick != null)
        {
            OnTick(this, new TimerEventArgs() {TickTime = DateTime.Now.ToLongTimeString()});
        }
    }

}





public class TimerEventArgs : EventArgs
{
    public string TickTime;
}




Valery.




Valery.


请参阅此CodeProject文章.
See this CodeProject article.Updating the UI from a thread - The simplest way[^]


这篇关于从类函数访问UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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