【问题标题】:Update UI from dynamic created class从动态创建的类更新 UI
【发布时间】:2015-03-18 08:49:30
【问题描述】:

我正在动态创建几个类和标签。我想从我的班级更新标签文本,但我不知道如何实现它。

我的班级:

  public partial class ScannerUtility : INotifyPropertyChanged
{
    public System.Timers.Timer timerHeartBeat = new System.Timers.Timer();
    public DateTime lastHeartBeat = new DateTime();
    public string heartBeatMessage = string.Empty;
    public event PropertyChangedEventHandler PropertyChanged;


    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    public void EnableScannerUtility()
    {
        timerHeartBeat = new System.Timers.Timer();
        timerHeartBeat.AutoReset = true;
        timerHeartBeat.Interval = 5000;// 35000;
        timerHeartBeat.Elapsed += TimerHeartBeat_Elapsed;
        timerHeartBeat.Start();
    }

    private void TimerHeartBeat_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        TimeSpan timeSinceLastHeartbeat = DateTime.Now.ToUniversalTime() - lastHeartBeat;
        if (timeSinceLastHeartbeat > TimeSpan.FromSeconds(30))
        {
            HeartBeatMessage = "No Answer from Scanner";
        }
        else
        {
            HeartBeatMessage = "Scanner OK";
        }
    }

    public string HeartBeatMessage
    {
        get { return this.heartBeatMessage; }

        set
        {
            if (value != this.heartBeatMessage)
            {
                this.heartBeatMessage = value;
                NotifyPropertyChanged("heartBeatMessage");
            }
        }
    }


}

以及我从主窗体创建它的循环:

 private void CreateSckanners()
    {
       foreach (BarCodeNodes item in iBarcodeScanners)
        {
            ScannerUtility util = new ScannerUtility();
            util.EnableScannerUtility();

            Label lbl = new Label();
            lbl.Text = item.IP.ToString();
            lbl.Name = item.IP.ToString();
            lbl.DataBindings.Add("Text", util, "HeartBeatMessage", false, DataSourceUpdateMode.OnPropertyChanged);
            flowLayoutPanel1.Controls.Add(lbl);
            flowLayoutPanel1.Update();
        }
    }

我希望在计时器结束时更新标签。

【问题讨论】:

    标签: c# winforms user-interface


    【解决方案1】:

    我担心你不能在你的情况下使用DataBindigns。你必须坚持InvokeRequired 风格。 Here, SO question 关于更新另一个线程中的控件。

    【讨论】:

    • 可以通过正确使用SynchronizationContext。但是,我总是宁愿避免我的后端类从另一个线程触发事件——在很少的情况下你真的必须这样做。
    【解决方案2】:

    bokibeg 是对的。我把代码放上以防有人感兴趣。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    
    namespace WindowsFormsApplication1
    {
     public partial class Form1 : Form
     {
      public Form1()
      {
       InitializeComponent();
       this.Load += new EventHandler(Form1_Load);
      }
    
      void Form1_Load(object sender, EventArgs e)
      {
       ScannerUtility util = new ScannerUtility(SynchronizationContext.Current);
       util.EnableScannerUtility();
       util.HeartBeatMessage = "Waiting for scanner...";
    
       this.SuspendLayout();
       Label lbl = new Label();
       lbl.Text = "Waiting for scanner...";
       lbl.Name = "lblTimer";
       lbl.Location = new System.Drawing.Point(15, 15);
       lbl.AutoSize = true;
       lbl.DataBindings.Add("Text", util, "HeartBeatMessage", false, DataSourceUpdateMode.OnPropertyChanged);
       this.Controls.Add(lbl);
       this.ResumeLayout(true);
      }
     }
    
     public partial class ScannerUtility : INotifyPropertyChanged
     {
      private SynchronizationContext _uiThreadContext;
      public System.Timers.Timer timerHeartBeat = new System.Timers.Timer();
      public DateTime lastHeartBeat = new DateTime();
      public string heartBeatMessage = string.Empty;
      public event PropertyChangedEventHandler PropertyChanged;
    
      public ScannerUtility(SynchronizationContext uiThreadContext)
      {
       _uiThreadContext = uiThreadContext;
      }
    
      private void NotifyPropertyChanged(String info)
      {
       if (PropertyChanged != null)
       {
        _uiThreadContext.Post(onUIPropertyChanged, new PropertyChangedEventArgs(info));
       }
       ;
      }
    
      private void onUIPropertyChanged(object state)
      {
        PropertyChanged(this, (PropertyChangedEventArgs)state);
      }
    
      public void EnableScannerUtility()
      {
       timerHeartBeat = new System.Timers.Timer();
       timerHeartBeat.AutoReset = true;
       timerHeartBeat.Interval = 5000;// 35000;
       timerHeartBeat.Elapsed += TimerHeartBeat_Elapsed;
       timerHeartBeat.Start();
      }
    
      private void TimerHeartBeat_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
      {
       TimeSpan timeSinceLastHeartbeat = DateTime.Now.ToUniversalTime() - lastHeartBeat;
       if (timeSinceLastHeartbeat > TimeSpan.FromSeconds(30))
       {
        HeartBeatMessage = "No Answer from Scanner";
       }
       else
       {
        HeartBeatMessage = "Scanner OK";
       }
      }
    
      public string HeartBeatMessage
      {
       get
       {
        return this.heartBeatMessage;
       }
    
       set
       {
        if (value != this.heartBeatMessage)
        {
         this.heartBeatMessage = value;
         NotifyPropertyChanged("HeartBeatMessage");
        }
       }
      }
     }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-06-28
      • 2019-05-10
      • 2018-09-26
      • 2022-01-09
      • 1970-01-01
      • 2011-01-21
      • 1970-01-01
      • 1970-01-01
      • 2020-04-12
      相关资源
      最近更新 更多