【问题标题】:type or namespace 'Timers' name does not exist in the namespace 'System'命名空间“系统”中不存在类型或命名空间“计时器”名称
【发布时间】:2013-03-17 03:21:51
【问题描述】:

我确信这个问题的答案远没有我的大脑想象的那么难,但我似乎无法弄清楚这一点。

我正在尝试为我正在开始的游戏设置一个计时器,主要是通过一个教程来了解基础知识。我什至用 EXACT 代码作为教程创建了一个新项目,但我不断收到相同的错误。尽管在教程中看到代码运行完美......

如果你能告诉我我在哪里搞砸了以及为什么,而不仅仅是更正代码,我将不胜感激。我将其作为一个学习项目进行,因此我想知道将来如何防止这种情况。提前致谢!

哦,我不知道这是否重要,但我使用的是 Microsoft Visual C# 2010 Express。

4 个错误,相同的消息。第 10、66、67 和 72 行。

using System;
using System.Collections.Generic;

namespace rout3reset
{
public class RogueLike
{
    static bool isGameAlive = true; 
    static Player player;
    static System.Timers.Timer World; //timer declare

    public class Player //
    {
        public char CHR = '@'; // Symbol for player
        public Vector position; // X, Y coordinates tracker
        public string Name = "";
        public short Health = 25; // Base health
        public short Mana = 25; // Base Mana
        public Player() //player constructor
        {
            position = new Vector(1, 1); // sets spawn point
        }
        public void Update() // get handle controls
        {

        }
    }
    public class AI //
    {

    }

    public class Tree //
    {

    }

    public class Vector
    {
        public short X;
        public short Y;
        public Vector(short X, short Y) // Main constructor
        {
            this.X = X;
            this.Y = Y;
        }
        public Vector() // Overload of 1
        {
            this.X = 0;
            this.Y = 0;
        }
    }

    static void Main(string[] args)
    {
        Initialize();
        do
        {
            player.Update();
        }
        while (isGameAlive);
    }

    static void Initialize()
    {
        World = new System.Timers.Timer(50); //Sets timer elapse point (50 milliseconds)
        World.Elapsed += new System.Timers.ElapsedEventHandler(Draw); //Start timer
        World.Start();
        player = new Player();
    }

    static void Draw(object sender, System.Timers.ElapsedEventArgs e) // Handles world timer tick
    {
        Console.Clear();
        Console.WriteLine("########################################"); //40 Spaces wide by 20 spaces tall
        Console.WriteLine("#--------------------------------------#");
        Console.WriteLine("#--------------------------------------#");
        Console.WriteLine("#--------------------------------------#");
        Console.WriteLine("#--------------------------------------#");
        Console.WriteLine("#--------------------------------------#");
        Console.WriteLine("#--------------------------------------#");
        Console.WriteLine("#--------------------------------------#");
        Console.WriteLine("#--------------------------------------#");
        Console.WriteLine("#--------------------------------------#");
        Console.WriteLine("#--------------------------------------#");
        Console.WriteLine("#--------------------------------------#");
        Console.WriteLine("#--------------------------------------#");
        Console.WriteLine("#--------------------------------------#");
        Console.WriteLine("#--------------------------------------#");
        Console.WriteLine("#--------------------------------------#");
        Console.WriteLine("#--------------------------------------#");
        Console.WriteLine("#--------------------------------------#");
        Console.WriteLine("#--------------------------------------#");
        Console.WriteLine("########################################");

        Console.WriteLine("-{0} HP: {1} MP: {2}", player.Name, player.Health, player.Mana); //STAT BAR  

        Console.SetCursorPosition(player.position.X, player.position.Y); //sets proper char position for player symbol
        Console.Write(player.CHR); //draws the players char
    }
    static void Updateworld() //update non-player, non-antagonist objects (trees, etc)
    {

    }

}

}

【问题讨论】:

  • @JonathonReinhart - 是的,我刚刚注意到了。
  • 这是什么类型的项目? (WinForms、WPF、控制台等)您的目标是什么版本的 .NET?也许您缺少对 System.dll 的引用 检查解决方案资源管理器中的项目引用文件夹以获取对 System 的引用
  • 在我的情况下,相同的代码构建良好。
  • express 安装 .net 框架似乎有什么问题?从 1.1 版开始,这已包含在 System.dll 中:msdn.microsoft.com/en-us/library/system.timers.timer.aspx

标签: c# visual-studio-2010 timer namespaces system


【解决方案1】:

MSDN 告诉我们System.Timers.Timer 来自 System.dll。

命名空间:System.Timers

程序集:系统(在 System.dll 中)

听起来您缺少对System.dll 的引用。确保你引用它:

【讨论】:

  • 不是每个新项目都包含这些内容吗?我猜你可能会意外删除它......
  • @David 是的,应该是。但这是我能想到的唯一会导致此错误的事情。
  • 谢谢你们!我现在正在研究它......我的解决方案资源管理器中没有一个参考,所以我猜这就是问题所在......我什至没有注意到。但希望这能奏效。
  • @user2214232 我将重新开始并创建一个新项目。谁知道还有什么坏了。然后把你的源文件拿过来。
  • @user2214232 - 不要忘记将 Jonathon 的答案标记为已接受,这样他才能获得荣誉。欢迎来到 SO,祝您编码愉快!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-19
  • 1970-01-01
  • 2012-05-28
相关资源
最近更新 更多