首页 > 解决方案 > 添加使用 DirectInput 设备时 WPF 应用程序挂起

问题描述

我曾经使用 Microsoft.DirectX.DirectInput.Device 类将操纵杆添加到我的 WinForms 应用程序中。现在我想用 WPF 做同样的事情。

这是我正在使用的一个非常简单的代码示例。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Interop;

using Microsoft.DirectX.DirectInput;

namespace JoysticksTest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // HANGS WHEN ENTERING DetectInstalledDevices METHOD!!!
            DetectInstalledDevices();
        }

        private void DetectInstalledDevices()
        {
            // Find all the GameControl devices that are attached.
            DeviceList gameControllerList = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly);

            // check that we have at least one device.
            if (gameControllerList.Count > 0)
            {
                for (int i = 0; i < gameControllerList.Count; i++)
                {
                    // Move to the first device
                    gameControllerList.MoveNext();

                    DeviceInstance deviceInstance = (DeviceInstance)gameControllerList.Current;

                    if (deviceInstance.DeviceType == DeviceType.Joystick)
                    {
                        // Some code here   
                    }
                }
            }
        }
    }
}

但我的 WPF 应用程序只是挂起。

我究竟做错了什么?我的电脑上安装了 Windows 8.1 操作系统。

是 WPF 还是 Microsoft.DirectX.DirectInput.dll 的错误版本?

标签: wpfdirectx

解决方案


推荐阅读