Myo hub 初始化期间未触发调度程序

Dispatcher not triggered during Myo hub initialization

我正在将 Myo 臂带的初始化代码移植到 WPF 应用程序,该应用程序使用 C# 包装器,http://goo.gl/HfwqQe 与设备交互。

但是当我在我的用户控件后面的代码中的 InitializeComponent(); 下添加初始化代码时,永远不会触发更新具有连接状态的文本框的行,this.Dispatcher.Invoke((Action)(() =>

我通过在调度程序代码之前的行上设置一个断点来调试它,这被称为 hub.MyoConnected += (sender, e) => 意味着 Myo 已连接,但随后的 dispatcher 行更新了statusTbx从未被调用和跳过。

这里有人知道是什么原因造成的吗?

我不确定为什么它不会将连接状态输出到文本框。之前使用的代码相同,但这是我正在使用的 C# 包装器的新版本。

控制台示例工作正常,http://goo.gl/RFHLym 并输出到控制台的连接,但我无法让它输出到文本框的连接。

这是获取 Myo 臂环连接状态的完整代码:

using MyoSharp.Communication;
using MyoSharp.Device;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MyoTestv4
{
    /// <summary>
    /// Interaction logic for ProductsView.xaml
    /// </summary>
    public partial class AdductionAbductionFlexionView : UserControl
    {
        public AdductionAbductionFlexionView()
        {
            InitializeComponent();



            // create a hub that will manage Myo devices for us
            using (var channel = Channel.Create(ChannelDriver.Create(ChannelBridge.Create())))
            using (var hub = Hub.Create(channel))
            {
                 //set a bpoint here, gets triggered
                // listen for when the Myo connects
                hub.MyoConnected += (sender, e) =>
                {
                     //set a bpoint here, doesn't get triggered
                    this.Dispatcher.Invoke((Action)(() =>
                    {
                        statusTbx.Text = "Myo has connected! " + e.Myo.Handle;
                        //Console.WriteLine("Myo {0} has connected!", e.Myo.Handle);
                        e.Myo.Vibrate(VibrationType.Short);

                    }));
                };

                // listen for when the Myo disconnects
                hub.MyoDisconnected += (sender, e) =>
                {
                    this.Dispatcher.Invoke((Action)(() =>
                    {
                        statusTbx.Text = "Myo has disconnected!";
                        //Console.WriteLine("Oh no! It looks like {0} arm Myo has disconnected!", e.Myo.Arm);
                        e.Myo.Vibrate(VibrationType.Medium);
                    }));
                };

                // start listening for Myo data
                channel.StartListening();
            }
        }
    }
}

您收到此错误是因为 channelhub 在调用

后立即被释放
channel.StartListening();

using 是为您处理对象的便捷方式,在这种情况下,这是不需要的。详情请参考using Statement (C# Reference)

尝试这些步骤来解决问题。 1. 将 channel 和 hub 声明为 class 的私有字段。 2. 不要使用using关键字。 3. 销毁AdductionAbductionFlexionView时记得销毁hubchannel

public partial class AdductionAbductionFlexionView : UserControl
{
    IChannel channel;
    IHub hub;

    public AdductionAbductionFlexionView()
    {
        InitializeComponent();

        // create a hub that will manage Myo devices for us
        channel = Channel.Create(ChannelDriver.Create(ChannelBridge.Create()));
        hub = Hub.Create(channel);

        //set a bpoint here, gets triggered
        // listen for when the Myo connects
        hub.MyoConnected += (sender, e) =>
        {
            //set a bpoint here, doesn't get triggered
            this.Dispatcher.Invoke((Action)(() =>
            {
                statusTbx.Text = "Myo has connected! " + e.Myo.Handle;
                //Console.WriteLine("Myo {0} has connected!", e.Myo.Handle);
                e.Myo.Vibrate(VibrationType.Short);

            }));
        };

        // listen for when the Myo disconnects
        hub.MyoDisconnected += (sender, e) =>
        {
            this.Dispatcher.Invoke((Action)(() =>
            {
                statusTbx.Text = "Myo has disconnected!";
                //Console.WriteLine("Oh no! It looks like {0} arm Myo has disconnected!", e.Myo.Arm);
                e.Myo.Vibrate(VibrationType.Medium);
            }));
        };

        // start listening for Myo data
        channel.StartListening();
    }
}