私は自分のアプリケーションをMVVM
パターンに移植しており、ビューの背後にあるコードから独自のmodel
クラスにコードを移動することから始めました。
私が行った最初のステップは、デバイスのネットワーク コードhttps://www.thalmic.com/en/myo/を MyoDevice クラスに移動することです。
元のコードは、ビューのコード ビハインドですべてのネットワーク コードをホストしていましたが、これは悪い習慣であると言われています。
Visual Studio で「Extract to Method」ツールを使用しようとしましたが、エラーが発生し続けます。"The selected text is not inside a method"
この接続コードと切断コードを 2 つの別々の方法に抽出する方法を知っている人はいますか?
デバイス接続コードを独自のモデルに移動する前のクラスは、もともと次のようになっていました。
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();
}