c# - “System.Windows.Forms.Timer”无法解析

标签 c# .net wpf winforms

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 引用后收到此错误消息。

最佳答案

由于您使用的是 Wpf,因此我制作了快速工作示例。确保您的项目引用看起来像这样。

enter image description here

主窗口

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 窗体计时器,Dispatcher 计时器在这种情况下可以正常工作,因为这是一个 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();
    }


}

关于c# - “System.Windows.Forms.Timer”无法解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13863159/

相关文章:

c# - 在代码中引用 XAML 元素时出现 WPF NullReferenceException

c# - YouTube上传API-离线获取Auth token

C# 使用 System.Data.Metadata.Edm

c# - 使用模拟时互斥创建挂起

c# - .Net 中 "kind of"公共(public)方法的一些编译器帮助

wpf - 我可以在WinForms中使用WPF控件来实现透明效果吗?

c# - 重 I/O 操作中的 Parallel.ForEach 与异步 For 循环

c# - 带计时器的动画盒速度更快

.net - 当我不知道记录是否存在时,如何使用 Entity Framework 进行合并?

c# - WPF DataGrid - "Ctrl-F"类型功能