已连接无线Xbox手柄但UWP中Gamepad.Gamepads为空,请求技术排查
Hey Jacob, sorry to hear you're stuck on this error—let's walk through the most likely fixes since your controller works elsewhere, so it's almost certainly a UWP-specific setup issue.
1. You're Missing the Required Device Capability
UWP apps need explicit permission to access game controllers, even if the system recognizes the device. If you haven't added this, your app can't see the controller at all.
To fix this:
- Open your
Package.appxmanifestfile. - Either use the visual editor: Go to the Capabilities tab and check the Game Controller box.
- Or edit the XML directly, add this inside the
<Capabilities>section:<DeviceCapability Name="gameController"/>
Save the manifest and rebuild your app—this is the most common fix for this exact error.
2. You're Trying to Access the Controller Too Early
The Gamepad.Gamepads collection might not populate immediately when your app starts, even if the controller is already connected. Calling First() on an empty collection throws that "Sequence contains no elements" error.
Instead of grabbing the controller on app launch, use the built-in events to react when a controller connects:
using Windows.Gaming.Input; // Register these events when your app initializes (e.g., in MainPage constructor) Gamepad.GamepadAdded += OnGamepadAdded; Gamepad.GamepadRemoved += OnGamepadRemoved; private void OnGamepadAdded(object sender, Gamepad e) { // Now you have a valid controller reference to work with var connectedGamepad = e; // Start reading input here or store the reference for later } private void OnGamepadRemoved(object sender, Gamepad e) { // Handle disconnection if needed }
If you still want to check for existing controllers at launch, always guard against empty collections first:
if (Gamepad.Gamepads.Any()) { var gamepad = Gamepad.Gamepads.First(); // Your input logic here } else { // Show a message asking the user to connect a controller }
3. Re-Pair Your Bluetooth Controller
Even if the controller shows up in Bluetooth settings, sometimes the UWP subsystem doesn't recognize the connection properly. Try re-pairing to refresh the link:
- Hold the pairing button on top of the controller until the Xbox logo flashes.
- Go to Windows Settings > Bluetooth & devices > Add device > Bluetooth, and re-select your controller.
Wait for the pairing to complete fully before launching your app again.
4. Update Your Windows Version
Older builds of Windows 10 had bugs with UWP access to Xbox Wireless Controllers. Make sure you're on the latest stable Windows 10/11 update—this can resolve low-level compatibility issues that nothing else fixes.
Give these steps a try, and let me know if you still run into issues!
内容的提问来源于stack exchange,提问作者Jacob

