C# ';System.Windows.Forms.Timer';无法解决

C# ';System.Windows.Forms.Timer';无法解决,c#,.net,wpf,winforms,C#,.net,Wpf,Winforms,错误1无法解析由类型“System.Windows.Forms.Timer”引用的基类或接口“System.ComponentModel.Component”系统,版本=4.0.0.0,区域性=中性,PublicKeyToken=b77a5c561934e089” k:\Program Files(x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Windows.Forms.dll App2 添加sys

错误1无法解析由类型“System.Windows.Forms.Timer”引用的基类或接口“System.ComponentModel.Component”系统,版本=4.0.0.0,区域性=中性,PublicKeyToken=b77a5c561934e089”

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


添加
system.windows.forms
reference后,我收到此错误消息。

尝试手动将引用添加到
system

1) 右键单击您的项目。单击
卸载项目

2) 右键单击卸载的项目。单击
Edit.csproj

3) 找到包含所有
项目组
,并在新行中添加


4) 保存文件,右键单击项目,然后单击重新加载项目,因为您使用的是Wpf I Make quick working示例。确保您的项目引用如下所示

主窗口

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();
        }
    }
}

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


如果您使用WINFALSE定时器,则考虑使用WPF代替。

您是否在同一个程序集中引用了<代码>系统> /代码>(V4),其中添加了<代码> Stury.Windows?窗体< /代码>引用?是的。我有三封推荐信。net、windows、windows.Forms@roken询问的是您是否有对
System.dll
的引用,因为这是类型未解析的程序集。这是什么类型的应用程序,如果它是Winform应用程序,它应该可以正常工作。@AndySurya我不确定.net指的是什么。您是否有意混合WPF和WinForms?这个错误表示您没有对系统的引用,或者至少没有正确的版本。啊,那太不幸了!您没有制作Metro应用程序,是因为Metro应用程序不支持
System.ComponentModel.Component
。为什么还要使用winforms计时器?对WPF使用Dispatchermer。@罗肯你是对的,我只是想回答OP的问题。@AndySurya上面的代码对我有用。您需要使用Windows.Forms.Timer有什么原因吗?@MarkHall我想在WindowsBase.dll中创建一个类似于@AndySurya的名称空间(MSDN文档告诉您必须引用哪个程序集)。
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();
    }


}