Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WPF应用程序中读取Xbox控制器_C#_Wpf_Visual Studio_Xbox - Fatal编程技术网

C# 在WPF应用程序中读取Xbox控制器

C# 在WPF应用程序中读取Xbox控制器,c#,wpf,visual-studio,xbox,C#,Wpf,Visual Studio,Xbox,是否可以在WPF应用程序中读取Xbox One控制器 我正在通过USB电缆连接它。我想从按钮中获取布尔值,并能够从棒和触发器中读取模拟值。我将使用这些值来控制 有没有一个简单的方法可以做到这一点?试试这个。Code4Fun可能会帮助您使用控制器 网址: WPF应用程序代码的主要视图如下所示 using System; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows;

是否可以在WPF应用程序中读取Xbox One控制器

我正在通过USB电缆连接它。我想从按钮中获取布尔值,并能够从棒和触发器中读取模拟值。我将使用这些值来控制


有没有一个简单的方法可以做到这一点?

试试这个。Code4Fun可能会帮助您使用控制器

网址:

WPF应用程序代码的主要视图如下所示

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Threading;
using SharpDX.XInput;

namespace ElBruno.GameController
{
    public partial class MainWindow : INotifyPropertyChanged
    {
        DispatcherTimer _timer = new DispatcherTimer();
        private string _leftAxis;
        private string _rightAxis;
        private string _buttons;
        private Controller _controller;

    public MainWindow()
    {
        DataContext = this;
        Loaded += MainWindow_Loaded;
        Closing += MainWindow_Closing;
        InitializeComponent();
        _timer = new DispatcherTimer {Interval = TimeSpan.FromMilliseconds(100)};
        _timer.Tick += _timer_Tick;
        _timer.Start();
    }

    void _timer_Tick(object sender, EventArgs e)
    {
        DisplayControllerInformation();
    }

    void DisplayControllerInformation()
    {
        var state = _controller.GetState();
        LeftAxis = string.Format("X: {0} Y: {1}", state.Gamepad.LeftThumbX, state.Gamepad.LeftThumbY);
        RightAxis = string.Format("X: {0} Y: {1}", state.Gamepad.RightThumbX, state.Gamepad.RightThumbX);
        //Buttons = string.Format("A: {0} B: {1} X: {2} Y: {3}", state.Gamepad.Buttons.ToString(), state.Gamepad.LeftThumbY);
        Buttons = string.Format("{0}", state.Gamepad.Buttons);

    }

    void MainWindow_Closing(object sender, CancelEventArgs e)
    {
        _controller = null;
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        _controller = new Controller(UserIndex.One);
        if (_controller.IsConnected) return;
        MessageBox.Show("Game Controller is not connected ... you know ;)");
        App.Current.Shutdown();
    }

    #region Properties

    public string LeftAxis
    {
        get
        {
            return _leftAxis;
        }
        set
        {
            if (value == _leftAxis) return;
            _leftAxis = value;
            OnPropertyChanged();
        }
    }

    public string RightAxis
    {
        get
        {
            return _rightAxis;
        }
        set
        {
            if (value == _rightAxis) return;
            _rightAxis = value;
            OnPropertyChanged();
        }
    }

    public string Buttons
    {
        get
        {
            return _buttons;
        }
        set
        {
            if (value == _buttons) return;
            _buttons = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}
}

您可以使用DirectInput或类似工具。这一个允许相对容易的任何输入设备的交互。如果Xbox控制器不使用DirectInput,它使用默认的USB驱动程序,您可以使用