2017-11-09 7 views
0

コマンドにバインドされたConnectButtonがあります。彼のCommand ImでDevicePickerを呼び出し、DeviceSelectedイベントをメソッドに設定します。変更方法DevicePicker.DeviceSelectedのイベントからのプロパティ

private void connectButtonCommand() 
    { 
     DevicePicker myDevicePicker = new DevicePicker(); 
     myDevicePicker.Show(new Rect(10, 10, 200, 200)); 
     //myDevicePicker.Filter.SupportedDeviceSelectors.Add(BluetoothDevice.GetDeviceSelectorFromPairingState(false)); 
     myDevicePicker.Filter.SupportedDeviceSelectors.Add(BluetoothDevice.GetDeviceSelectorFromPairingState(true)); 
     myDevicePicker.DeviceSelected += MyDevicePicker_DeviceSelected; 


    } 

と方法

private async void MyDevicePicker_DeviceSelected(DevicePicker sender, DeviceSelectedEventArgs args) 
    { 
     var deviceInfo = args.SelectedDevice as DeviceInformation; 
     Device = deviceInfo; 

     BluetoothController = new BluetoothController.BluetoothController(); 

     var list = await BluetoothController.FindPairedDevicesAsync(); 

     RefcommDevice = list.First(x => x.Id.Contains(Device.Id)); 

     List<DeviceInformation> deviceList = new List<DeviceInformation>(); 

     deviceList = list.ToList(); 

     Device = deviceList[0]; 

     connected = await BluetoothController.ConnectAsync(RefcommDevice); 

     isConnected = connected; 

     IsConnectedEvent += new IsConnectedEentHandler(Device_IsConnected); 

     if (IsConnected) 
     { 
      if(IsConnectedEvent != null) 
       IsConnectedEvent(); 
     } 



     paired = true; 

    } 

Device_IsConnected方法は、このようないくつかのプロパティを設定する必要があります。

private void Device_IsConnected() 
    { 
      ConnectButtonVisibility = Visibility.Collapsed; 
      OnButtonVisibility = Visibility.Visible; 
      OffButtonVisibility = Visibility.Visible; 
      ReadDatafButtonVisibility = Visibility.Visible; 

    } 

ティが機能していません。次のエラーメッセージが表示されます。

System.Runtime.InteropServices.COMException(0x8001010E):スレッドのマーシャル戦争のために、スレッドのマーシャル戦争が始まりました。 (HRESULTからの例外:0x8001010E(RPC_E_WRONG_THREAD))System.StubHelpers.EventArgsMarshaler.CreateNativePCEventArgsInstance(文字列名)で System.Runtime.InteropServices.WindowsRuntime.PropertyChangedEventArgsMarshaler.ConvertToNative(PropertyChangedEventArgs managedArgs)で System.ComponentModel.PropertyChangedEventHandler.Invokeで (オブジェクト送信者、PropertyChangedEventArgs E)ArduinoDistance.ViewModels.StartPageViewModelでArduinoDistance.ViewModels.StartPageViewModel.set_ConnectButtonVisibility(ビジビリティ値) でArduinoDistance.ViewModels.BaseViewModel.SetProperty [T](T &ストレージ、T値は、文字列のPropertyName) で。 Device_IsConnected()

ここでこれを解決する簡単な方法は?私はそれをconnectButtonCommand()のwhile(!IsConnected)のセットで知って解決し、UIを変更します。しかし、私はthiが最良の方法だと思う。

EDITは を使用して問題を解決:アプリはできません。

await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => 
     testMethod()); 

おかげ

Agredoエラーが言うように

答えて

0

を、問題は、コールが発生したスレッドです非UIスレッドからUI要素を操作するこの場合、問題はUI以外のスレッドでUI相対コードを含むDevice_IsConnectedメソッドを呼び出すことです。

これを修正するには、Dispatcher.RunAsyncを呼び出して、これらのメソッドの呼び出しをUIスレッドに移動します。たとえば:

await Window.Current.Dispatcher.RunAsync(
    Windows.UI.Core.CoreDispatcherPriority.Normal, async() => 
{ 
    // Your UI update code goes here! 
    Device_IsConnected(); 
}); 

詳細はCoreDispatcherを参照してください。

+0

これは動作しません。 \t Window.Current == nullおそらく私は私のViewModelにいるので? – Agredo

+0

ありがとうございます。あなたの答えはとても助けになりました! – Agredo

関連する問題