2013-08-30 97 views
3

我正在尝试构建一个简单的秒表WPF应用程序。找不到引用System.Diagnostics.Stopwatch

这里是我的代码:

using System; 
using System.Collections.Generic; 
using System.Configuration; 
using System.Data; 
using System.Linq; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Diagnostics; 

namespace WpfApplication1 
{ 
/// <summary> 
/// Interaction logic for App.xaml 
/// </summary> 
public partial class App : Application 
{ 
    public stopWatch = new Stopwatch(); 

    private void startTimer() 
    { 
     stopWatch.Start(); 
     Dispatcher.BeginInvoke(DispatcherPriority.Render, new ThreadStart(ShowElapsedTime)); 
    } 
    void ShowElapsedTime() 
    { 
     TimeSpan ts = stopWatch.Elapsed; 
     lblTime.Text = String.Format("{0:00}:{1:00}.{2:00}", ts.Minutes, ts.Seconds, ts.Milliseconds/10); 
    } 
} 
} 

这里

enter image description here

我使用System.Diagnostics程序,但由于某种原因,我无法访问Stopwatch

而且我可以在此对话中找不到System.Diagnostics:

enter image description here

为什么我不能使用System.Diagnostics.Stopwatch以及为什么System.Diagnostics不出现在引用对话框中?

+1

代码是文本。当你将它复制并粘贴为文本时,比在图片中显示时更容易看到(在当前屏幕上显示的内容很小)。 –

+0

'System.Diagnostics'在'System.dll'内。我会创建一个新的空白控制台应用程序 - >验证您可以在那里访问它。另外,当你说你不能使用它...什么是错误?我们看不到任何错误。 – Arran

+2

您在声明“stopWatch”字段时犯了一个错误;你忘了声明类型。 :)编辑:C#语法/ intellisense检查器已经疯了,因为它。 –

回答

8

你需要:

Stopwatch stopWatch = new Stopwatch(); 

你必须(public stopWatch = new StopWatch),这是不如何创建C#对象。

stopWatch是实例,StopWatch是类定义。