从微软手环中获取心率

5

我正在尝试从Microsoft Band获取心率。每当值发生变化时,它都应该进行更新。然后,我尝试将该值显示在TextBlock中。我首先创建了一个IBandClient的实例,并设置其HeartRate.ReadingChanged方法如下:

bandClient.SensorManager.HeartRate.ReadingChanged += HeartRate_ReadingChanged;

然后我尝试像这样更新值:

private void HeartRate_ReadingChanged(object sender, Microsoft.Band.Sensors.BandSensorReadingEventArgs<Microsoft.Band.Sensors.IBandHeartRateReading> e)
{
    HeartRate = e.SensorReading.HeartRate;
}

HeartRate是一个像这样设置的int类型:

public int HeartRate
{
    get { return (int)GetValue(HeartRateProperty); }
    set { SetValue(HeartRateProperty, value); }
}

// Using a DependencyProperty as the backing store for HeartRate.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty HeartRateProperty =
    DependencyProperty.Register("HeartRate", typeof(int), typeof(MainPage), new PropertyMetadata(0));

然后将TextBlock文本绑定到HeartRate。然而,当尝试设置HeartRate时,我一直收到此错误信息:

应用程序调用了为不同线程进行了编组的接口。(来自HRESULT的异常:0x8001010E (RPC_E_WRONG_THREAD))

我猜测它正在尝试在上一个调用仍在进行中时设置HeartRate

你知道SensorManager是否正在从另一个线程分派事件吗?这意味着你正在尝试从不同于UI线程的线程设置TextBlock文本,这是不允许的(因此会出现错误)。 - y2bd
1个回答

3

尝试实现这个功能并查看结果,如果您仍需要int变量,则在文本块中显示时将其转换回字符串。

await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, 
               () => 
               {
                   Textblock.Text =  e.SensorReading.HeartRate.ToString())
               }).AsTask();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接