我正在将Myo臂章的初始化代码移植到WPF应用程序,该应用程序使用C#包装器http://goo.gl/HfwqQe与设备交互。
但是,当我在用户控件后面的代码中添加InitializeComponent();下的初始化代码时,永远不会触发更新具有连接状态的文本框的行,this.Dispatcher.Invoke((Action)(() =>。
我在dispatcher代码之前在行上设置一个断点来调试它,它被称为hub.MyoConnected += (sender, e) =>,这意味着Myo已经连接,但是在更新之后的dispatcher行永远不会调用和跳过statusTbx。
有人知道这是什么导致的吗?
我不知道为什么它不会将连接状态输出到textbox。这段代码以前同样有效,但这是我正在使用的C#包装器的一个新版本。
控制台示例运行良好,http://goo.gl/RFHLym和输出连接到控制台,但我无法让它输出到textbox的连接。
这是获取Myo arm波段连接状态的完整代码:
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();
}
}
}
}发布于 2015-01-09 15:21:43
您将得到此错误,因为channel和hub在调用后立即被释放。
channel.StartListening();using是一种方便的方式来为您释放对象,在这种情况下,这是不需要的。请参考使用语句(C#引用)获得更多信息。
尝试以下步骤来解决问题。1.将通道和集线器声明为类的私有字段。2.不要使用using关键字。3.当hub和channel被释放时,记住要处理AdductionAbductionFlexionView和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();
}
}https://stackoverflow.com/questions/27863413
复制相似问题