簡體   English   中英

'System.Windows.Forms.Timer'無法解析

[英]'System.Windows.Forms.Timer' could not be resolved

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我做了快速工作的例子。 確保您的項目引用如下所示。

在此輸入圖像描述

主窗口

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


}

如果您只使用WPF,則沒有理由引用System.Windows.Forms(WinForms)。 這是兩種不同的技術,除非必要,否則我不建議將它們混合使用。

如果您使用的是WinForms Timer,請考慮使用WPF的DispatcherTimer

嘗試手動添加對System的引用。

1)右鍵單擊您的項目。 單擊Unload Project

2)右鍵單擊卸載的項目。 單擊Edit <YourProjectName>.csproj

3)找到包含所有<Reference Include="AssemblyName">ItemGroup ,並在新行上添加<Reference Include="System" />

4)保存文件並右鍵單擊項目,然后單擊“ Reload Project

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM