Revit API WinForms - 将 ComboBox 值传回 Command

Revit API WinForms - Passing ComboBox value back to Command

我正在尝试将一个值(元素 ID)从 WinForm 传回 Command.cs 文件,但出现错误:

 System.NullReferenceException: Object reference not set to an instance of an object.
   at BatchSheetMaker.Command.Execute(ExternalCommandData commandData, String& message, ElementSet elements)

我正在关注这里的 youtube 教程,它看起来相当简单和直接,但传回 Command.cs 是另一层复杂性。

我将 Command.cs 代码包裹在 try/catch 块中,它只告诉我有 nullReferenceException,但它没有告诉我它发生在哪一行。我已经 looked around 但没有找到任何关于如何使调试显示错误行的提示。如果有人有任何其他指示,那会很有帮助。

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 button1Continue_Click(object sender, EventArgs e)
        {
            MyVal = comboBox1TitleBlockList.Text;
        }

Command.cs

    Form1 form1 = new Form1(commandData);
    String elementString = form1.MyVal.ToString();
    Element eFromString = doc.GetElement(elementString);
    ElementId titleBlockId = eFromString.Id;

    ViewSheet sheet = ViewSheet.Create(doc, titleBlockId);


Run your entire add-in inside the Visual Studio debugger 并逐行执行您的代码。这将向您准确显示抛出异常的位置,并使您能够轻松识别导致问题的原因。

将我的代码更改为此并开始工作:

form1.cs

 public string MyVal;
        //{

            //get { return myVal; }
            //set { myVal = value; }
        //}

this link was helpful 以及 youtube 上有关如何在表单之间传递值的教程。