获取电池信息报告更新 MVVM windows 10
Get Battery Information reportUpdated MVVM windows 10
As I can call reportUpdated event from my ViewModel?
<code>
public MainPage()
{
this.InitializeComponent();
Battery.AggregateBattery.ReportUpdated += AggregateBattery_ReportUpdated;
}
</code>
几个选项...
- 在 VM 中附加事件处理程序,而不是使用代码隐藏。编辑:正如评论中所指出的,这不是有史以来最干净的 MVVM 解决方案。一种更简洁的方法是在您的 VM 中使用某种平台独立接口 (IAggregateBattery),并通过构造函数注入提供平台实现。
- 从 AggregateBattery_ReportUpdated.
在您的 VM 上调用一个方法
- 当事件发生时使用某种信使发送消息(MVVM Light 有,其他框架可能也有)。
我找到了解决方案:
在构造函数中我的 ViewModel
<code>
public MainViewModel()
{
//The firs time, launch to method for get the current status battery
this.RequestAggregateBatteryReport();
// This event is launch each time, when the status battery change
Battery.AggregateBattery.ReportUpdated += AggregateBattery_ReportUpdated1;
}
private async void AggregateBattery_ReportUpdated1(Battery sender, object args)
{
//The Dispatcher is used when in other thread because is important use the other thread for avoid issue.
Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
RequestAggregateBatteryReport();
});
}
private void RequestAggregateBatteryReport()
{
// Create aggregate battery object
var aggBattery = Battery.AggregateBattery;
// Get report
var report = aggBattery.GetReport();
// Update UI
AddReportUI(report, aggBattery.DeviceId);
}
private void AddReportUI(BatteryReport report, string DeviceID)
{
this.BatteryEnergy = new BatteryEnergy();
// Disable progress bar if values are null
if ((report.FullChargeCapacityInMilliwattHours == null) ||
(report.RemainingCapacityInMilliwattHours == null))
{
this.BatteryEnergy.ProgressBarMaxium = 0.0;
this.BatteryEnergy.ProgressBarValue = 0.0;
this.BatteryEnergy.ProgressBarPorcent = "N/A";
}
else
{
this.BatteryEnergy.ProgressBarMaxium = Convert.ToDouble(report.FullChargeCapacityInMilliwattHours);
this.BatteryEnergy.ProgressBarValue = Convert.ToDouble(report.RemainingCapacityInMilliwattHours);
this.BatteryEnergy.ProgressBarPorcent = ((Convert.ToDouble(report.RemainingCapacityInMilliwattHours) / Convert.ToDouble(report.FullChargeCapacityInMilliwattHours)) * 100).ToString("F2") + "%";
}
}
As I can call reportUpdated event from my ViewModel?
<code>
public MainPage()
{
this.InitializeComponent();
Battery.AggregateBattery.ReportUpdated += AggregateBattery_ReportUpdated;
}
</code>
几个选项...
- 在 VM 中附加事件处理程序,而不是使用代码隐藏。编辑:正如评论中所指出的,这不是有史以来最干净的 MVVM 解决方案。一种更简洁的方法是在您的 VM 中使用某种平台独立接口 (IAggregateBattery),并通过构造函数注入提供平台实现。
- 从 AggregateBattery_ReportUpdated. 在您的 VM 上调用一个方法
- 当事件发生时使用某种信使发送消息(MVVM Light 有,其他框架可能也有)。
我找到了解决方案:
在构造函数中我的 ViewModel
<code>
public MainViewModel()
{
//The firs time, launch to method for get the current status battery
this.RequestAggregateBatteryReport();
// This event is launch each time, when the status battery change
Battery.AggregateBattery.ReportUpdated += AggregateBattery_ReportUpdated1;
}
private async void AggregateBattery_ReportUpdated1(Battery sender, object args)
{
//The Dispatcher is used when in other thread because is important use the other thread for avoid issue.
Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
RequestAggregateBatteryReport();
});
}
private void RequestAggregateBatteryReport()
{
// Create aggregate battery object
var aggBattery = Battery.AggregateBattery;
// Get report
var report = aggBattery.GetReport();
// Update UI
AddReportUI(report, aggBattery.DeviceId);
}
private void AddReportUI(BatteryReport report, string DeviceID)
{
this.BatteryEnergy = new BatteryEnergy();
// Disable progress bar if values are null
if ((report.FullChargeCapacityInMilliwattHours == null) ||
(report.RemainingCapacityInMilliwattHours == null))
{
this.BatteryEnergy.ProgressBarMaxium = 0.0;
this.BatteryEnergy.ProgressBarValue = 0.0;
this.BatteryEnergy.ProgressBarPorcent = "N/A";
}
else
{
this.BatteryEnergy.ProgressBarMaxium = Convert.ToDouble(report.FullChargeCapacityInMilliwattHours);
this.BatteryEnergy.ProgressBarValue = Convert.ToDouble(report.RemainingCapacityInMilliwattHours);
this.BatteryEnergy.ProgressBarPorcent = ((Convert.ToDouble(report.RemainingCapacityInMilliwattHours) / Convert.ToDouble(report.FullChargeCapacityInMilliwattHours)) * 100).ToString("F2") + "%";
}
}