Windows 表单关闭试图传递值后卡在蓝色微调器中

Stuck in blue Spinner after Windows Form closed trying to pass value

我正在尝试从我正在尝试设置的 WinForms 传回一个值,但是一旦关闭表单,单击继续按钮后 Revit 就会卡在蓝色微调器中并且不会 return 到 运行 原始程序。

我已经四处查看,但没有找到任何可以 return 加入程序的东西。大多数(如果不是全部)WinForms 教程与处理 Revit 无关。我拥有的大多数 found/watched 处理将值从一种形式传递到另一种形式,尽管 I found this one 尽管它有点不同。

这是我的代码:

Form1.cs

public partial class Form1 : System.Windows.Forms.Form
    {
        private UIApplication uiapp;
        private UIDocument uidoc;
        private Autodesk.Revit.ApplicationServices.Application app;
        private Document doc;

        private string myVal;

        public string MyVal
        {
            get { return myVal; }
            set { myVal = value; }
        }

        public Form1(ExternalCommandData commandData)
        {
            InitializeComponent();

            uiapp = commandData.Application;
            uidoc = uiapp.ActiveUIDocument;
            app = uiapp.Application;
            doc = uidoc.Document;
    }

    public delegate void delPassData(System.Windows.Forms.ComboBox text);

        private void Form1_Load(object sender, EventArgs e)
        {
            //Create a filter to get all the title block types.
            FilteredElementCollector colTitleBlocks = new FilteredElementCollector(doc);
            colTitleBlocks.OfCategory(BuiltInCategory.OST_TitleBlocks);
            colTitleBlocks.WhereElementIsElementType();

            foreach(Element x in colTitleBlocks)
            {
                comboBox1TitleBlockList.Items.Add(x.Name);
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1Continue_Click(object sender, EventArgs e)
        {

            MyVal = comboBox1TitleBlockList.Text;  // is returning with Titleblock.Name

            button1Continue.DialogResult = DialogResult.OK;
            Debug.WriteLine("OK button was clicked.");
            Close();
            //Dispose();
            return;
        }

        private void button2Cancel_Click(object sender, EventArgs e)
        {
            button2Cancel.DialogResult = DialogResult.Cancel;
            Debug.WriteLine("Cancel button was clicked");
            //Dispose();

            Close();
            return;
        }

        private void comboBox1TitleBlockList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }

Command.cs

 Form1 form1 = new Form1(commandData);

 System.Windows.Forms.Application.Run(new Form1(commandData)); // not sure if this line is required.

 form1.ShowDialog();

 String elementString = form1.MyVal; //<---- this is returning null. need to figure out how to pass value from form1 back to Command.cs

if (elementString != null)
   {
       elementString = form1.MyVal.ToString();
       Element eFromString = doc.GetElement(elementString);
       titleBlockId = eFromString.Id;                                
   }
   else
   {
        titleBlockId = collector.FirstElementId();
   }

感谢所有help/direction。

这永远行不通。你说 not sure if this line is required 试图......我什至无法描述它试图实现的目标。

Revit API 纯粹是事件驱动的,您的 Revit 加载项只能从有效的 Revit API 上下文调用 Revit API。此类上下文仅在您与 Revit 事件连接的事件处理程序中提供。

您的代码没有订阅任何此类事件。因此,它不是 Revit 插件。

请完成 Revit API getting started material 并构建符合 Revit 附加模块架构要求的附加模块。