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