0

我正在将 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();
            }
        }
    }
}
4

1 回答 1

2

您收到此错误是因为channelhub在调用后立即处理

channel.StartListening();

using是为您处理对象的便捷方式,在这种情况下,这是不希望的。有关详细信息,请参阅using 语句(C# 参考)

尝试这些步骤来解决问题。1.将channel和hub声明为类的私有字段。2.不要使用using关键字。3. 记得处置hub以及channel何时AdductionAbductionFlexionView处置。

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();
    }
}
于 2015-01-09T15:21:43.717 回答