1

游戏杆.cs

using System;
using Microsoft.DirectX.DirectInput;

namespace gameproject
{
    /// <summary>
    /// Description of device.
    /// </summary>
    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,那么这个错误是什么意思?

4

1 回答 1

2

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

类似的stackoverflow帖子: 调试错误“类型'xx'是在未引用的程序集中定义的”

于 2011-04-09T10:52:38.097 回答