【问题标题】:'System.Windows.Forms.Timer' could not be resolved'System.Windows.Forms.Timer' 无法解析
【发布时间】:2012-12-13 15:32:52
【问题描述】:

Error 1 The base class or interface 'System.ComponentModel.Component' in assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' referenced by type 'System.Windows.Forms.Timer' could not be resolved

k:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Windows.Forms.dll App2

我在添加 system.windows.forms 引用后收到此错误消息。

【问题讨论】:

  • 您在添加System.Windows.Forms 引用的同一程序集中是否有对System (v4) 的引用?
  • 是的。我有3个参考。 .net , windows , windows.forms
  • @roken 询问的是您是否引用了System.dll,因为这是未解析类型的程序集。
  • 这是什么类型的应用程序,如果是 Winform 应用程序应该可以正常工作。
  • @AndySurya 我不确定“.net”指的是什么。您是否有意混合 WPF 和 WinForms?该错误表明您没有对 System 的引用,或者至少没有正确的版本。

标签: c# .net wpf winforms


【解决方案1】:

由于您使用的是 Wpf,因此我制作了快速工作示例。确保您的项目引用如下所示。

主窗口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Forms;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Timer tmr = new Timer();
        public MainWindow()
        {
            InitializeComponent();
            tmr.Interval = 2000;
            tmr.Tick += new EventHandler(tmr_Tick);
            tmr.Start();
        }

        void tmr_Tick(object sender, EventArgs e)
        {
            tmr.Stop();
            throw new NotImplementedException();
        }
    }
}

正如 roken 所说,如果你可以使用 Wpf Dispatcher Timer 会更容易。在查看示例链接时,没有迫切需要使用 Windows 窗体计时器,由于这是一个 WPF 程序,因此调度程序计时器在这种情况下可以正常工作。

根据您的链接修改修改

public partial class MainWindow : Window
{
    System.Windows.Threading.DispatcherTimer tmrStart = new System.Windows.Threading.DispatcherTimer();
    System.Windows.Threading.DispatcherTimer tmrStop = new System.Windows.Threading.DispatcherTimer();
    public MainWindow()
    {
        InitializeComponent();
        tmrStart.Interval = TimeSpan.FromSeconds(2); //Delay before shown
        tmrStop.Interval = TimeSpan.FromSeconds(3);  //Delay after shown
        tmrStart.Tick += new EventHandler(tmr_Tick);
        tmrStop.Tick += new EventHandler(tmrStop_Tick);

    }

    void tmrStop_Tick(object sender, EventArgs e)
    {
        tmrStop.Stop();
        label1.Content = "";
    }

    void tmr_Tick(object sender, EventArgs e)
    {
        tmrStart.Stop();
        label1.Content = "Success";
        tmrStop.Start();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        tmrStart.Start();
    }


}

【讨论】:

  • 为什么还要使用 winforms 计时器?将 DispatcherTimer 用于 WPF。
  • @roken 你是对的,我只是想按要求回答 OP 的问题。
  • @AndySurya 上面的代码对我有用。您是否有需要使用 Windows.Forms.Timer 的原因?
  • @MarkHall 我想做类似this
【解决方案2】:

如果您专门使用 WPF,则没有理由引用 System.Windows.Forms (WinForms)。这是两种不同的技术,除非必要,否则我不建议将它们混合使用。

如果您使用的是 WinForms 计时器,请考虑改用 WPF 的 DispatcherTimer

【讨论】:

  • @AndySurya 该命名空间位于 WindowsBase.dll 中(MSDN 文档告诉您必须引用哪个程序集)。
【解决方案3】:

尝试手动添加对System 的引用。

1) 右键单击​​您的项目。点击Unload Project

2) 右键单击​​已卸载的项目。点击Edit &lt;YourProjectName&gt;.csproj

3) 找到包含所有&lt;Reference Include="AssemblyName"&gt;s 的ItemGroup 并将&lt;Reference Include="System" /&gt; 添加到新行。

4) 保存文件并右键单击您的项目并单击Reload Project

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-14
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
相关资源
最近更新 更多