0

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?

Jacob
  • 363
  • 4
  • 12

1 Answers1

2

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();
}
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • I want to add more info from the documentation: *Gamepad supports any Xbox One certified or Xbox 360 compatible gamepad.* – mihkov Jun 09 '18 at 17:15