我正在将我的应用程序移植到一个MVVM
模式中,并首先将代码从我的视图后面的代码移动到它自己的model
类中。
我采取的第一步是将设备的网络代码https://www.thalmic.com/en/myo/移动到 MyoDevice 类。
原始代码在后面的代码中托管了所有网络代码,我被告知这是不好的做法。
我尝试在 Visual Studio 中使用“提取到方法”工具,但我不断收到错误消息:"The selected text is not inside a method"
有谁知道我如何将此连接和断开代码提取到两个单独的方法中?
在将设备连接代码移动到它自己的模型之前,该类最初看起来像这样:
http://hastebin.com/gepudayele.cs
这是放入 MyoDevice 模型后的代码:
http://hastebin.com/ocogoseziy.cs
例如用于连接和断开连接的代码,用于在设备连接/断开连接时监听设备:
// create a hub that will manage Myo devices for us
channel = Channel.Create(ChannelDriver.Create(ChannelBridge.Create()));
hub = Hub.Create(channel);
{
// listen for when the Myo connects
hub.MyoConnected += (sender, e) =>
{
this.Dispatcher.Invoke((Action)(() =>
{
statusTbx.Text = "Myo has connected! " + e.Myo.Handle;
e.Myo.Vibrate(VibrationType.Short);
// unlock the Myo so that it doesn't keep locking between our poses
e.Myo.Unlock(UnlockType.Hold);
e.Myo.PoseChanged += Myo_PoseChanged;
e.Myo.OrientationDataAcquired += Myo_OrientationDataAcquired;
}));
};
// listen for when the Myo disconnects
hub.MyoDisconnected += (sender, e) =>
{
this.Dispatcher.Invoke((Action)(() =>
{
statusTbx.Text = "Myo has disconnected!";
e.Myo.Vibrate(VibrationType.Medium);
e.Myo.OrientationDataAcquired -= Myo_OrientationDataAcquired;
e.Myo.PoseChanged -= Myo_PoseChanged;
}));
};
// start listening for Myo data
channel.StartListening();
}