I'm currently working on a UWP app and I want to be able to take input from a wireless xbox controller however, whenever I try and access it in my code, I get the error System.InvalidOperationException: 'Sequence contains no elements'
.
I know that the controller is connected because it shows up in my bluetooth devices and I can use it in Steam Big Picture and in games without a problem. I'm trying to access the controller using this code:
var controller = Gamepad.Gamepads.First();
var reading = controller.GetCurrentReading();
Am I missing something or doing something wrong?
I want to be able to take input from a wireless xbox controller however, whenever I try and access it in my code, I get the error System.InvalidOperationException: 'Sequence contains no elements'.
From official document:
the gamepad list is initally empty and will not list gamepads even if they are already connected. After a short period this will return a complete list of gamepads.
For this scenario, you could list all connected gamepads through the Gamepad.GamepadAdded
event.
public MainPage()
{
this.InitializeComponent();
Gamepad.GamepadAdded += Gamepad_GamepadAdded;
}
private void Gamepad_GamepadAdded(object sender, Gamepad e)
{
var controller = Gamepad.Gamepads?.First();
var reading = controller.GetCurrentReading();
}