【问题标题】:Dispatcher not triggered during Myo hub initialization在 Myo 集线器初始化期间未触发调度程序
【发布时间】:2015-03-07 22:36:54
【问题描述】:

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

【问题讨论】:

  • using (var hub = Hub.Create(channel))? hubchannel.StartListening(); 之后立即被销毁。
  • 去掉 using 子句再试一次,看看是否有效。
  • @kennyzx 当我删除 using 子句时,hubchannel 出现错误,因为它们找不到。
  • 我的意思是删除 using 关键字。 using 表示对象是一次性的,并且在使用括号结束时调用其 Dispose() 方法。所以事件不会被触发。只需致电var channel = Channel.Create(ChannelDriver.Create(ChannelBridge.Create());
  • @kennyzx 我在这两行代码之前删除了这个关键字,但是我得到了错误,var and hub doesn't existOnly assignment, call, increment, decrement, await, and new object expressions can be used as a statement

标签: c# wpf user-interface dispatcher myo


【解决方案1】:

您收到此错误是因为 channelhub 在调用后立即被释放

channel.StartListening();

using 是为您处理对象的便捷方式,在这种情况下,这是不希望的。更多信息请参考using Statement (C# Reference)

尝试这些步骤来解决问题。 1.将channel和hub声明为类的私有字段。 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();
    }
}

【讨论】:

  • 在这一行`channel = Channel.Create(ChannelDriver.Create(ChannelBridge.Create()));` 我收到一个错误cannot implicitly convert type 'MyoSharp.Communication.IChannel' to 'MyoSharp.Communication.Channel' are you missing a cast? 我也收到同样的错误IHub 在下一行.知道这是为什么吗?
  • 分别声明为IChannel channel; IHub hub;
  • 我更新了我的答案,以提供有关原因和修复的更多解释。
  • 上述解决方案有效,但是当我打电话时,我尝试调用事件处理程序时,dispathcer 代码不会更新文本框,poseStatusTbx 你知道可能出了什么问题吗?这是完整的问题:stackoverflow.com/questions/27953938/…
  • 我正在尝试将此代码移动到它自己的类并创建两个方法,connectdisconnect 但我不确定如何将它们提取到方法中。如果您有任何意见,我在这里发布了一个关于它的问题:goo.gl/ZOmDAa
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-08
  • 2014-04-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多