【问题标题】:namespace name 'DispatcherTimer' could not be found找不到命名空间名称“DispatcherTimer”
【发布时间】:2013-04-01 04:51:28
【问题描述】:

我正在尝试使用调度计时器,但我的 c# 应用程序找不到命名空间。这是错误:

找不到类型或命名空间名称“DispatcherTimer”(您是否缺少 using 指令或程序集引用?)

这是我正在使用的:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Text;
using System.Threading;
using System.Timers;
using System.Windows.Forms;

代码如下:

DispatcherTimer timer1 = new DispatcherTimer();

【问题讨论】:

  • 您是否在项目引用中为此命名空间添加了引用?根据this DispatcherTimer 生活在System.Windows.Threading 命名空间中。
  • 我没有添加的选项,system.windows 之后的选择只有表单和xps

标签: c# reference namespaces .net-assembly dispatchertimer


【解决方案1】:

DispatcherTimer 不是命名空间 - 它是 System.Windows.Threading 命名空间和 WindowsBase 程序集中的一个类。所以你需要

using System.Windows.Threading;

一般来说,搜索缺少的类型名称和“MSDN”就足以找出在哪里可以找到类型。

【讨论】:

  • 我没有添加的选项,system.windows 之后的选择只有forms和xps
  • @nate:听起来您不是在编写 WPF 或 Silverlight 应用程序 - 听起来您是在编写 Windows 窗体应用程序,而 DispatcherTimer 不是为此而生的。请查看System.Windows.Forms.Timer
  • 哦,我以为你可以在任何项目类型中使用 dispatchertimer。
  • 好吧,好吧,我想我会给我们一些别的东西。感谢您的帮助
  • @nate:我已经建议使用“其他东西” - System.Windows.Forms.Timer。这是一个总是在 UI 线程上触发的计时器,我认为这就是你所追求的。
【解决方案2】:

不在 System.Windows.Threading 命名空间中,也不在 System.Threading 命名空间中?

http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.aspx

【讨论】:

  • 我没有添加的选项,system.windows 之后的选择只有表单和xps
【解决方案3】:

添加这个使用:

using System.Windows.Threading;

MSDN

【讨论】:

  • 我没有添加的选项,system.windows 之后的选择只有表单和xps
【解决方案4】:

Dispatcher 类位于 WindowsBase.dll 中。您必须将该引用添加到您的项目才能使用“使用 System.Windows.Threading;”

【讨论】:

    【解决方案5】:

    您必须在引用中添加 WindowsBase,以便您可以使用 System.Windows.Threading。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-28
      • 1970-01-01
      • 2016-06-14
      • 2017-07-12
      相关资源
      最近更新 更多