0

我正在为 Autodesk Inventor 创建一个插件。基本上,您定义要添加的按钮,并告诉应用程序添加按钮定义。我遇到的问题是,当我为按钮定义定义“OnExecute”方法时,按钮不会执行。我认为我试图组织代码的方式是造成问题的原因。

我有一个 CustomButton 类,它有一个看起来像这样的委托属性(签名是无效的,带有NameValueMap接口的输入)

public class CustomButton
{
    // … properties and methods that don't matter here
    public ButtonDefinitionSink_OnExecuteEventHandler Execute { get; set; }
}

在 mainActivate()方法(Inventor 启动时调用的方法)中,我创建了以下类的实例来设置所有按钮定义以及单击它们时触发的方法。该类如下所示:

public class CustomButtonDefinitions
{
    public CustomButtonDefinitions(ref Application app)
    {
        _inventorApp = app;
        InitializeButtonDefinitions();
    }

    public List<CustomButton> CustomButtons { get; set; } = new List<CustomButton>();

    private void InitializeButtonDefinitions()
    {
        AddTestButton();
    }
    private void AddTestButton()
    {
        var testButton = new CustomButton
        {
            DisplayName = "test",
            InternalName = "testCommand1",
            Ribbon = "Assembly",
            RibbonPanel = "Simplification",
            IconSource = "./Assets/test.jpg",
            Classification = CommandTypesEnum.kFileOperationsCmdType,
            ShowText = true,
            UseLargeIcon = true,
        };

        testButton.Execute = TestButton_Execute;
        CustomButtons.Add(testButton);
    }
    private void TestButton_Execute(NameValueMap Context)
    {
        // This is where the logic of the button would go.
        // For now, just something that gives me an indication it worked.
        System.Windows.Forms.MessageBox.Show("Hello");
        _inventorApp.ActiveDocument.Close();
    }
}

我认为错误的来源是下一个代码(这是在Activate()

CustomButtonDefinitions customButtonDefinitions = new CustomButtonDefinitions(ref _InventorApp);
foreach (var button in customButtonDefinitions.CustomButtons)
{
            // this creates the button in Inventor
    var buttonDef = button.CreateButtonDefinition(ref controlDefs);
    // and this subscribes the button click event to my method  
    buttonDef.OnExecute += button.Execute;
}

必须有一些东西从按钮单击事件中取消订阅我的方法。

我也将在 Inventor 论坛上发布此内容,但我也想在这里查看,因为我是代理和事件处理程序的新手。我要么不了解委托/事件,要么是特定于 Inventor 的东西,我需要一些其他帮助。

希望这足以提供一些背景信息。提前致谢。

4

1 回答 1

0

问题是我没有在足够高的范围内创建按钮定义。我需要在方法范围之上创建一个变量,Activate()以便应用程序可以在需要时看到它。

于 2018-11-27T23:07:46.440 回答