Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.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
如何调整Cheet.NET以绑定到我的C#Form?_C#_Winforms_Nuget Package - Fatal编程技术网

如何调整Cheet.NET以绑定到我的C#Form?

如何调整Cheet.NET以绑定到我的C#Form?,c#,winforms,nuget-package,C#,Winforms,Nuget Package,因此,我在设置键绑定以在项目中执行某些操作时遇到了问题(右Alt+左控件) 我试着使用一个叫做Cheet.NET的API,它说它可以很容易地调整一个项目,并允许定制“konami”代码来调用函数 Cheet.NET的(小型)文档: 因此,我在VisualStudio中使用NuGet软件包管理器安装了它,保存并重新加载了我的项目 回来后,尝试编写一个简单的keybind,我已经从初始化代码中得到了很多错误: //初始化 var cheet=新cheet(); myUIElement.Previe

因此,我在设置键绑定以在项目中执行某些操作时遇到了问题(右Alt+左控件)

我试着使用一个叫做Cheet.NET的API,它说它可以很容易地调整一个项目,并允许定制“konami”代码来调用函数

Cheet.NET的(小型)文档:

因此,我在VisualStudio中使用NuGet软件包管理器安装了它,保存并重新加载了我的项目

回来后,尝试编写一个简单的keybind,我已经从初始化代码中得到了很多错误:


//初始化
var cheet=新cheet();
myUIElement.PreviewKeyDown+=cheet.OnKeyDown;
奶酪地图(“↑ ↑ ↓ ↓", ()=>{Debug.WriteLine(“瞧!”;});
这是我放置代码的地方:

namespace WindowsFormsApp1
{
公共部分类Form1:Form
{
//初始化
var cheet=新cheet();
myUIElement.PreviewKeyDown+=cheet.OnKeyDown;
奶酪地图(“↑ ↑ ↓ ↓“,()=>{Debug.WriteLine(“瞧!”;});
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
}
}
}
它无法识别很多代码,也给了我很多计算错误:

类、结构或接口成员声明中的标记“+=”无效
预期的类型
元组必须至少包含两个元素
无效令牌“”↑ ↑ ↓ ↓类、结构或接口成员声明中的“”

我听说我应该把包“绑定”到我的项目上,但我不知道怎么做

感谢以后的帮助。

更新(因为您已将错误输出添加到问题中):

类、结构或接口成员声明中的标记“+=”无效 预期类型

你不能只把C代码放在类的顶层,你必须遵循一个最小的结构。你可以把这样的代码放在构造函数方法或其他方法或类的静态初始化块中(如果你只在代码中引用静态内容)

下一次更新(更新错误后):

使用泛型类型“Cheet”需要1个类型参数

您使用的是通用软件包,而不是Wpf软件包,它们会:

public class Cheet : Cheet<Key>, ICheet
公共类Cheet:Cheet,ICheet
很不幸,我认为它们使用相同的类名

因此,需要使用不同的用法来引用该类(请参见其名称空间,它是CheetNET.Wpf): 而不是你用的奶酪网核心

更多信息:

从WEPF的CHIET类的版本中,唯一的WPF内容看起来是Stask.WiDOWS.Enter。它可能只在WiFrase/WPF互操作(见)中使用它,但是我会考虑为WrFrm写一个类似于WPF的类。 旧回复:

如果您刚刚在解决方案中安装了该软件包(我认为控制台中有一个下拉菜单来选择目标),请确保在Visual Studio中右键单击该项目并管理其Nuget软件包

也可以在“解决方案/源资源管理器”窗格中右键单击“解决方案/顶部”节点,然后从Nuget packages management(Nuget软件包管理)窗口中选择特定已安装软件包可用的项目

请注意,manager中有一个单独的选项卡,用于在线/可用、已安装和更新


不管怎样,当您是新手时,无需使用控制台,使用UI,更简单、更友好

存储库中有一个WPF演示应用程序,它让您在窗口构造函数中初始化:

public MainWindow()
{
    var cheet = new Cheet();
    PreviewKeyDown += cheet.OnKeyDown;

    cheet.Map("↑ ↑ ↓ ↓ ← → ← → b a", () => { WriteLine("Voilà!"); });

    cheet.Map("i d d q d", () => {
        WriteLine("god mode enabled");
    });
    [etc.]
因此,您可以使用
cheet.OnKeyDown
处理
PreviewKeyDown
事件,并且假定在
OnKeyDown
中,cheet将循环遍历其映射并寻找适合的映射

我设置了一个测试WinForms项目,并添加了Cheet.NET,如果您想在WinForms中使用它,看起来您还有一些工作要做

Cheet.Core
有一个
Cheet
类,但它是抽象的。看起来
T
是一个“key”类型。Cheet.Wpf库有一个
Cheet
类,使用Wpf
key
类型继承自
Cheet

似乎您需要创建自己的
Cheet
类继承(很可能)自
Cheet


我想现在的问题是,哪一项工作更重要:实现Cheet for WinForms,还是在WPF中重新启动项目。

最简单的方法是,您几乎可以从github复制/粘贴WPF实现,并更改为使用适用于windows窗体的正确事件处理程序/枚举:

using CheetNET.Core;
using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;

public class Cheet : Cheet<System.Windows.Forms.Keys>
{
    private static readonly Regex LetterKeysNamePattern = new Regex(@"^[a-z]$");
    private static readonly Regex NumberKeysNamePattern = new Regex(@"^[0-9]$");
    private static readonly Regex KeyspadNumberKeysNamePattern = new Regex(@"^kp_[0-9]$");
    private static readonly Regex FunctionKeysNamePattern = new Regex(@"^(?:f[1-9]|f1[0-2])$");

    private PreviewKeyDownEventArgs lastHandledEvent;

    public virtual void OnKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        if (e == lastHandledEvent)
        {
            return;
        }
        OnKeyDown(e.KeyCode);
        lastHandledEvent = e;
    }

    protected override Keys GetKey(string KeysName)
    {
        if (LetterKeysNamePattern.IsMatch(KeysName))
        {
            return ParseKey(KeysName.ToUpper());
        }
        if (NumberKeysNamePattern.IsMatch(KeysName))
        {
            return ParseKey("D" + KeysName);
        }
        if (KeyspadNumberKeysNamePattern.IsMatch(KeysName))
        {
            return ParseKey(KeysName.Replace("kp_", "NumPad"));
        }
        if (FunctionKeysNamePattern.IsMatch(KeysName))
        {
            return ParseKey(KeysName.ToUpper());
        }
        switch (KeysName)
        {
            case "left":
            case "L":
            case "←":
                return Keys.Left;
            case "up":
            case "U":
            case "↑":
                return Keys.Up;
            case "right":
            case "R":
            case "→":
                return Keys.Right;
            case "down":
            case "D":
            case "↓":
                return Keys.Down;
            case "backspace":
                return Keys.Back;
            case "tab":
                return Keys.Tab;
            case "enter":
                return Keys.Enter;
            case "return":
                return Keys.Return;
            case "shift":
            case "⇧":
                return Keys.LShiftKey;
            case "control":
            case "ctrl":
            case "^":
                return Keys.LControlKey;
            case "alt":
            case "option":
            case "⌥":
                return Keys.Alt;
            case "command":
            case "⌘":
                return Keys.LWin;
            case "pause":
                return Keys.Pause;
            case "capslock":
                return Keys.CapsLock;
            case "esc":
                return Keys.Escape;
            case "space":
                return Keys.Space;
            case "pageup":
                return Keys.PageUp;
            case "pagedown":
                return Keys.PageDown;
            case "end":
                return Keys.End;
            case "home":
                return Keys.Home;
            case "insert":
                return Keys.Insert;
            case "delete":
                return Keys.Delete;
            case "equal":
            case "=":
                return Keys.Oemplus;
            case "comma":
            case ",":
                return Keys.Oemcomma;
            case "minus":
            case "-":
                return Keys.OemMinus;
            case "period":
            case ".":
                return Keys.OemPeriod;
            case "kp_multiply":
                return Keys.Multiply;
            case "kp_plus":
                return Keys.Add;
            case "kp_minus":
                return Keys.Subtract;
            case "kp_decimal":
                return Keys.Decimal;
            case "kp_divide":
                return Keys.Divide;
        }
        throw new ArgumentException(String.Format("Could not map Keys named '{0}'.", KeysName));
    }

    private static Keys ParseKey(string KeysName)
    {
        return (Keys)Enum.Parse(typeof(Keys), KeysName);
    }
}

但是我如何在.NET中做到这一点呢forms@TaylorSpark我添加了更多的细节。不幸的是,它看起来像开箱即用的WinForms。哈,好吧……你把它放在了正确的位置。并且与指示的错误相匹配。那么现在的错误是什么呢?只是更新我们的答案并在以后对以前的答案进行向下投票似乎是徒劳的ying想从你之前所说的猜测问题,他们确实支持.NET。他们不支持windows窗体。你可以为winforms编写一个实现。但是你必须为winforms打开key preview,请参阅我评论中的链接above@GeorgeBirbiliscore中的
Cheet
是抽象的,WPF版本在Winforms中无法像我一样工作为密钥处理提供不同的API。repo@GeorgeBirbilis100%。除了有人真的不再使用winforms之外。它对新手来说更好,而且有一个遵循我非常喜欢的PME(Property Method Event,属性方法事件)模式的所见即所得编辑器(来自Delphi和classic VB)。如果有人把它移植到.NET Core上,在Linux等系统中使用,它现在也会变得更流行;-)legacy UIS的口号也是@jamice我只是觉得winforms很容易使用,直到最近我才听说过WPF
protected override void OnLoad(EventArgs e)
{
    var cheet = new Cheet();
    PreviewKeyDown += cheet.OnKeyDown;
    cheet.Map("c h e a t", () => { MessageBox.Show("Voilà!"); });

    base.OnLoad(e);
}