如何在XNA中使用DirectX.DirectInput

joystick.cs

using System; using Microsoft.DirectX.DirectInput; namespace gameproject { ///  /// Description of device. ///  class joysticks { public static Device joystick; public static JoystickState state; public static void InitDevices() //Function of initialize device { //create joystick device. foreach (DeviceInstance di in Manager.GetDevices( DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly)) { joystick = new Device(di.InstanceGuid); break; } if (joystick == null) { //Throw exception if joystick not found. } //Set joystick axis ranges. else { foreach (DeviceObjectInstance doi in joystick.Objects) { if ((doi.ObjectId & (int)DeviceObjectTypeFlags.Axis) != 0) { joystick.Properties.SetRange( ParameterHow.ById, doi.ObjectId, new InputRange(-5000, 5000)); } } joystick.Properties.AxisModeAbsolute = true; joystick.SetCooperativeLevel(null,CooperativeLevelFlags.NonExclusive | CooperativeLevelFlags.Background); //Acquire devices for capturing. joystick.Acquire(); state = joystick.CurrentJoystickState; } } public static void UpdateJoystick() // Capturing from device joystick { //Get Joystick State. if(joystick!=null) state = joystick.CurrentJoystickState; } } } 

在这一行中,发生了错误,

  joystick.SetCooperativeLevel(null,CooperativeLevelFlags.NonExclusive | CooperativeLevelFlags.Background); 

错误,

 Error 1 The type 'System.Windows.Forms.Control' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Windows.Forms... 

我正在研究,XNA 3.0和.NET 3.5,那么这意味着什么错误?

SetCooperativeLevelSystem.Windows.Forms.Control对象作为第一个参数(其中为null),因此您仍应引用在应用程序中定义此类的程序集。 添加引用从您的应用程序/游戏执行System.Windows.Forms.dll,然后尝试。 如果您使用的代码正在使用其他一些您没有引用的类,那就没关系,但是当它们是公共的(比如它们是参数或从您调用的方法返回)时,您必须引用其中的程序集那些类型是定义的。

类似的stackoverflow文章: 调试错误“类型’xx’在未引用的程序集中定义”