如何使标签显示cpu加载实时数据
本文关键字:加载 实时 数据 cpu 显示 何使 标签 | 更新日期: 2023-09-27 18:24:56
我知道如何使用性能计数器获取cpu负载值,但不知道如何让标签实时显示
将标签内容绑定到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();