gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:42:45 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com