Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# GetAsyncKeyState和using System.Windows.Forms不';行不通_C#_.net_Wpf_Windows_Get - Fatal编程技术网

C# GetAsyncKeyState和using System.Windows.Forms不';行不通

C# GetAsyncKeyState和using System.Windows.Forms不';行不通,c#,.net,wpf,windows,get,C#,.net,Wpf,Windows,Get,我的“键盘记录器”有问题。为什么在这段代码中我看到库“使用System.Windows.Forms 它显示以下错误: 命名空间“System.Windows”中不存在类型或命名空间名称“Forms”(是否缺少程序集引用? C:\Users\SoChi\documents\visual studio 2013\Projects\WPFAApplication8\WPFAApplication8\MainWindow.xaml.cs 16 22 WPFAApplication8 我不知道为什么。你能

我的“键盘记录器”有问题。为什么在这段代码中我看到库“使用System.Windows.Forms

它显示以下错误:

命名空间“System.Windows”中不存在类型或命名空间名称“Forms”(是否缺少程序集引用?
C:\Users\SoChi\documents\visual studio 2013\Projects\WPFAApplication8\WPFAApplication8\MainWindow.xaml.cs 16 22 WPFAApplication8

我不知道为什么。你能帮我吗

整个代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.IO;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using System.ComponentModel;
namespace WpfApplication8
{
    public partial class MainWindow : Window
    {
               public MainWindow()
        {
            InitializeComponent();
        }
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern Int16 GetAsyncKeyState(int vKey);
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            while (true)
            for (int key = 0; key < 255;key++)
                if (GetAsyncKeyState(key) == -32767)
                {
                    label.Content = "kliknales cos na klawiaturze";
                }
        }       
    }
}    `
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用System.IO;
使用System.Windows.Forms;
使用系统图;
使用System.Runtime.InteropServices;
使用系统组件模型;
命名空间WpfApplication8
{
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
[DllImport(“User32.dll”,CharSet=CharSet.Auto)]
公共静态外部程序Int16 GetAsyncKeyState(int vKey);
私有无效按钮\u单击(对象发送者,路由目标e)
{
while(true)
用于(int key=0;key<255;key++)
如果(GetAsyncKeyState(键)=-32767)
{
label.Content=“kliknales cos na klawiaturze”;
}
}       
}
}    `

为什么在WPF应用程序中需要
System.Windows.Forms
?由于WPF和WinForms是两种不同的技术,它们通常不会相互引用,因此您必须通过右键单击项目并选择
addreference…
,手动添加引用。如果没有
System.Windows.Forms,程序将无法运行。这段代码有什么不好的地方?我在创建模板后得到了这个System.Windows.Forms。问题出在别处。您的代码没有引用任何来自
System.Windows.Forms
的内容,因此您应该删除它。仅供参考,因为您在按钮中运行无限循环,UI将无法重新绘制自身(因此您将看不到标签更新)。研究使用一种新的方法。