【发布时间】:2015-11-30 01:28:42
【问题描述】:
我知道如何使用性能计数器获取 cpu 负载值,但不知道如何让标签实时显示它
【问题讨论】:
我知道如何使用性能计数器获取 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();
【讨论】: