【问题标题】:How can I make label display cpu load Real time data如何让标签显示 cpu 加载实时数据
【发布时间】:2015-11-30 01:28:42
【问题描述】:

我知道如何使用性能计数器获取 cpu 负载值,但不知道如何让标签实时显示它

【问题讨论】:

    标签: c# wpf label cpu


    【解决方案1】:

    将您的标签内容绑定到您的 ViewModel:

    XAML (YourView.xaml.cs):

    <Label Content="{Binding CPUText}" />
    

    您的视图模型如下所示:

    public class YourViewModel : INotifyPropertyChanged
    {
    
      public void GetCpuText()
      {
         //your code here.... 
         //it would populate your CPUText property...
    
         CPUText = .... (your code to get the cpu info)
      }
      private _cpuText;
    
      public string CPUText
      {
         get
         {
            return _cpuText;
         }
         set
         {
            _cpuText = value;
    
             NotifyPropertyChanged("CPUText");
         }
      }
    
      public event PropertyChangedEventHandler PropertyChanged;
    
      protected void NotifyPropertyChanged(String info) {
        if (PropertyChanged != null) {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
      }
    }
    

    实现这项工作的一个示例是创建您的视图,将该视图的 DataContext 设置为您的 ViewModel 类:

    var view = new YourView();
    view.DataContext = new YourViewModel();
    view.GetCpuText();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-18
      • 2020-03-12
      • 1970-01-01
      • 2010-10-19
      • 1970-01-01
      • 1970-01-01
      • 2011-06-04
      相关资源
      最近更新 更多