简体   繁体   English

如何调试“WindowsFormsAutoClick.Form1的类型初始化程序”引发异常。“?

[英]How to debug “Type initializer for 'WindowsFormsAutoClick.Form1' threw an exception.”?

I am trying to add a new Dictionary value and add a new User Control to a Flow Layout. 我正在尝试添加新的Dictionary值并将新的用户控件添加到Flow布局。 But pressing the add control button throws an initializer exception with an inner exception such as "Key has already been added". 但是按下添加控件按钮会抛出一个初始化异常,并带有内部异常,例如“已添加密钥”。 I don't know the full exception because I am having difficulties with finding how to open the popup error that usually shows in Visual Studio. 我不知道完整的异常,因为我在查找如何打开通常在Visual Studio中显示的弹出错误时遇到了困难。

Already set a loop to check if the key already is added to the dictionary and if so change the key (as shown in the code blocK). 已设置循环以检查密钥是否已添加到字典中,如果是,则更改密钥(如代码blocK中所示)。 Also tried clearing the flow layout controls list. 还尝试清除流布局控件列表。 (made no changes to the error) (没有更改错误)

Function were the error is thrown: 函数被抛出错误:

/// <summary>
    /// Create a new key propertie set.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
private void buttonAddKey_Click(object sender, EventArgs e)
{
    // Create new UI key component in the flow layout.
    KeyPropertiesCtrl inputKeyCtrl = new KeyPropertiesCtrl();
    flowLayoutKeys.Controls.Add(inputKeyCtrl);

    Console.WriteLine("inputKeyCtrl " + inputKeyCtrl.Name + " parent " + inputKeyCtrl.Parent.Name);

    // Create new data input key.
    CustomInputKey newKey = new CustomInputKey();
    newKey.Activation.InputKey = new Interception();

    Console.WriteLine("newKey " + newKey.Activation.InputKey);

    // Get a key binding from the user.
    try
    {
        Form1.Context = InterceptionDriver.CreateContext();

        InterceptionDriver.SetFilter(Form1.Context, InterceptionDriver.IsKeyboard, (Int32)KeyboardFilterMode.All);
        InterceptionDriver.SetFilter(Form1.Context, InterceptionDriver.IsMouse, (Int32)MouseFilterMode.All);

        Form1.InterceptOnce(Form1.Context, out newKey.Activation.InputKey.DeviceId, out newKey.Activation.InputKey.TheStroke);

        InterceptionDriver.DestroyContext(Form1.Context);


        // Change the key bind in UI text.
        if (newKey.Activation.InputKey.TheStroke.Key.State != 0)
            inputKeyCtrl.button1.Text = newKey.Activation.InputKey.TheStroke.Key.Code.ToString();
        else
            inputKeyCtrl.button1.Text = newKey.Activation.InputKey.TheStroke.Mouse.State.ToString();
    }
    catch (Exception excp)
    { Console.WriteLine(excp); }

    // Set the key bind in data list.
    string name = inputKeyCtrl.Name;
    byte attempts = 0;
    while (Form1.CurrentSelections.SelectedPreset.GetCustomKeys.ContainsKey(name))
    {
        if (attempts > 50)
        {
            break;
        }
        attempts++;
        name += attempts;
    }

    Console.WriteLine(this.Name + " name Attempts " + attempts + " name " + name);

    if (attempts < 50)
        Form1.CurrentSelections.SelectedPreset.AddKey(Form1.CurrentSelections.SelectedPresetName, name, newKey);
}

So What I actually expect is after pressing the button control. 所以我真正期望的是在按下按钮控制之后。 It creates a new User Control in a Flow Layout and then it freezes the application when it hits the function InterceptOnce() because there it will wait for me to press a input to bind that input. 它在流布局中创建一个新的用户控件,然后当它到达InterceptOnce()函数时冻结应用程序,因为它会等待我按下输入来绑定该输入。 Finally store the new key bind and save the dictionary on to a file. 最后存储新的密钥绑定并将字典保存到文件中。

But it throws the exception "key has already been added" on the line of while(Form1.CurrentSelections...){} and before I added that while loop and the try{} the exception was thrown on Form1.Context = InterceptionDriver.CreateContext(); 但它在while(Form1.CurrentSelections ...){}的行上引发异常“key has has added”,之后我添加了while循环和try {}异常抛出Form1.Context = InterceptionDriver。的createContext(); which is weird, because with the try{}catch(Exception excp){} it doesn't throw an exception. 这很奇怪,因为使用try {} catch(Exception excp){}它不会抛出异常。 I think the exception has to do with new CustomInputKey(); 我认为异常与新的CustomInputKey()有关; and have no clue why... 并且不知道为什么......

Error: screenshot 错误: 截图

Initializer error information is also shown in Class[Design]. 初始化程序错误信息也显示在类[设计]中。 Making the variable non-static will move when the exception is thrown which helped me finding the cause of the exception. 抛出异常会使变量非静态移动,这有助于我找到异常的原因。

Turns out I was assigning a value while in an initializing phase of a control component to a variable which wasn't created yet. 事实证明,我在控制组件的初始化阶段将值分配给尚未创建的变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么Class不断抛出“ MyClass的类型初始值设定项引发异常”。 - Why Class keeps throwing 'The type initializer for 'MyClass' threw an exception.' “Azure.Core.Pipeline.HttpClientTransport”的类型初始化程序引发了异常。 - The type initializer for “Azure.Core.Pipeline.HttpClientTransport” threw an exception.' 如何调试“我的类的类型初始化器”引发异常“ - How to debug “The type initializer for 'my class' threw an exception” System.TypeInitializationException: &#39;Bid&#39; 的类型初始值设定项引发异常。 - System.TypeInitializationException: 'The type initializer for 'Bid' threw an exception.' System.TypeInitializationException:&#39;类型初始值设定项引发了异常。 - System.TypeInitializationException: 'The type initializer threw an exception.' “Microsoft.AspNetCore.Mvc.MvcCoreLoggerExtensions”的类型初始值设定项引发异常。 - The type initializer for 'Microsoft.AspNetCore.Mvc.MvcCoreLoggerExtensions' threw an exception.' 如何最好地调试“ xx类的类型初始化器引发异常” - How best to debug “The type initializer for xx class threw an exception” 类型初始值设定项引发异常 - Type initializer threw an exception 如何修复错误“'的类型初始化程序?' 抛出异常。” 使用 EASendMail 通过 C# 发送电子邮件时? - How do I fix the error “The type initializer for '?' threw an exception.” when sending e-mails through C# with EASendMail? &#39;&#39;的类型初始值设定项引发了异常 - The type initializer for '' threw an exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM