如何调试"Type initializer for 'WindowsFormsAutoClick.Form1' threw an exception."?

How to debug "Type initializer for 'WindowsFormsAutoClick.Form1' threw an exception."?

我正在尝试添加一个新的字典值并向流布局添加一个新的用户控件。但是按下添加控制按钮会抛出一个带有内部异常的初始化程序异常,例如 "Key has already been added"。我不知道完整的异常,因为我很难找到如何打开通常显示在 Visual Studio.

中的弹出错误

已经设置了一个循环来检查键是否已经添加到字典中,如果是则更改键(如代码块所示)。 还尝试清除流程布局控件列表。 (未更改错误)

抛出错误的函数:

/// <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);
}

所以我真正期望的是在按下按钮控件之后。它在流布局中创建一个新的用户控件,然后在它点击函数 InterceptOnce() 时冻结应用程序,因为它会在那里等待我按下输入以绑定该输入。最后存储新键绑定并将字典保存到文件中。

但是它在 while(Form1.CurrentSelections...){} 行抛出异常 "key has already been added" 并且在我添加 while 循环和 try{} 之前抛出异常 Form1.Context = InterceptionDriver.CreateContext();这很奇怪,因为使用 try{}catch(Exception excp){} 它不会抛出异常。我认为异常与 new CustomInputKey(); 有关并且不知道为什么...

错误: screenshot

初始化程序错误信息也显示在Class[设计]中。当抛出异常时,使变量成为非静态变量将移动,这有助于我找到异常的原因。

结果我在控制组件的初始化阶段为尚未创建的变量赋值。