I have a ConnectButton binded to a Command. In his Command Im calling the DevicePicker and set the DeviceSelected Event to a method.
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;
}
and the Method
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;
}
The Device_IsConnected method should set some properties like this:
private void Device_IsConnected()
{
ConnectButtonVisibility = Visibility.Collapsed;
OnButtonVisibility = Visibility.Visible;
OffButtonVisibility = Visibility.Visible;
ReadDatafButtonVisibility = Visibility.Visible;
}
Thi is not working. I get the following error:
System.Runtime.InteropServices.COMException (0x8001010E): Eine Schnittstelle, die für einen anderen Thread marshalled war, wurde von der Anwendung aufgerufen. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD)) at System.StubHelpers.EventArgsMarshaler.CreateNativePCEventArgsInstance(String name) at System.Runtime.InteropServices.WindowsRuntime.PropertyChangedEventArgsMarshaler.ConvertToNative(PropertyChangedEventArgs managedArgs) at System.ComponentModel.PropertyChangedEventHandler.Invoke(Object sender, PropertyChangedEventArgs e) at ArduinoDistance.ViewModels.BaseViewModel.SetProperty[T](T& Storage, T Value, String PropertyName) at ArduinoDistance.ViewModels.StartPageViewModel.set_ConnectButtonVisibility(Visibility value) at ArduinoDistance.ViewModels.StartPageViewModel.Device_IsConnected()
Is there a simple way to solve this? I solved it know by set in connectButtonCommand() a while(!IsConnected) and make there my UI changes. But I think thi is ot the best way.
EDIT Solved the Problem by Using:
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
testMethod());
Thanks
Agredo
As the error says, the problem is the thread on which the call occurs: the app cannot manipulate UI elements from non-UI threads. In this case the problem is that you invoke the Device_IsConnected
method which contains UI relative code in the non-UI thread.
To fix this, move the calls to those methods to the UI thread by calling Dispatcher.RunAsync
. For example:
await Window.Current.Dispatcher.RunAsync(
Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
{
// Your UI update code goes here!
Device_IsConnected();
});
Details please reference CoreDispatcher
.
User contributions licensed under CC BY-SA 3.0