Initialize WPF window throw exception

Initialize WPF window throw exception

Anonymous
Not applicable
1,639 Views
2 Replies
Message 1 of 3

Initialize WPF window throw exception

Anonymous
Not applicable

Hi all,

I've been making a small plugin for my Revit project. The plugin will prompt user to a modeless dialog where they select variable to pass into the external event. The dialog is a WPF window, and I use MVVM pattern. I follow the modeless dialog sample in the SDK to initialize my window, however, when I debug my code, Revit kept throwing exception at the ExternalCommand step :"object reference not set to an instance of an object"

My code for ExternalApplication and ExternalCommand is as below, I did almost everything exactly as the example except passing externalevent and its handler to the view model:

class RevitCommand : IExternalCommand
    {        
        public virtual Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                ThisApplication.thisApp.ShowWindow(commandData.Application);

                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return Result.Failed;
            }
        }
    }

and the application class:

public class ThisApplication : IExternalApplication
    {
        //Class instance
        internal static ThisApplication thisApp;

        //Modeless instance
        private MainWindow m_MainWindow;

        public Result OnShutdown(UIControlledApplication application)
        {
            if (m_MainWindow != null && m_MainWindow.IsVisible)
            {
                m_MainWindow.Dispose();
            }
            return Result.Succeeded;
        }

        public Result OnStartup(UIControlledApplication application)
        {
            m_MainWindow = null;   // no dialog needed yet; the command will bring it
            thisApp = this;  // static access to this application instance
            return Result.Succeeded;
        }

        public void ShowWindow(UIApplication uiapp)
        {
            // If we do not have a dialog yet, create and show it
            if (m_MainWindow == null )
            {
                RequestHandler handler = new RequestHandler();
                ExternalEvent exEvent = ExternalEvent.Create(handler);
                MyViewModel vmod = new MyViewModel(exEvent,handler);
                m_MainWindow = new MainWindow();                
                m_MainWindow.DataContext = vmod;
                m_MainWindow.Show();
            }
        }

    }

I suspect the exception come from thisApp being null, however it cannot have any value at the beginning of initialization.

Any help or suggestion is much appreciated, thanks 

0 Likes
Accepted solutions (1)
1,640 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable
Accepted solution

I have figured it out, thisApp being null really was the problem, the ExternalApplication class was never invoke, and Revit throw the exception at the ExternalCommand level. I just need to make a new ExternalApplication class instance ,call the ShowWindow method and the program does what I expected:

public virtual Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                new ThisApplication().ShowWindow(commandData.Application);

                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return Result.Failed;
            }
        }

However, this is exactly how the example in SDK modeless dialog. I wonder why it work there?

0 Likes
Message 3 of 3

reylorente1
Collaborator
Collaborator
I am doing the Revit classes and WPF example? The first M in MVVM, and I want to do it with ExternalEvent.
Notice, that you placed the event in the ViewModel, How did you do it, or if you can give me a tip, to be able to do it, Thanks.

 

 

class ViewmodelRevitBridge : BindableBase
    {
        private Dictionary<string, int> _dicWallType;
        private int _selectedWallType;
        private ObservableCollection<string> _listParameters;

        // Declare the Revit model class here.
        // Consequently, create a get-set variable representing this.
        private Model.modelRevitBridge _revitModel;

        public Dictionary<string, int> DicWallType
        {
            get
            {
                return _dicWallType;
            }

            set
            {
                SetProperty(ref _dicWallType, value);
            }
        }

        public int SelectedWallType
        {
            get
            {
                return _selectedWallType;
            }

            set
            {
                SetProperty(ref _selectedWallType, value);
            }
        }

        public ObservableCollection<string> ListParameters
        {
            get
            {
                return _listParameters;
            }

            set
            {
                SetProperty(ref _listParameters, value);
            }
        }

        //  Commands
        // This will be used by the button in the WPF window.
        public ICommand RetrieveParametersValuesCommand
        {
            get
            {
                return new DelegateCommand(RetrieveParametersValuesAction);
            }
        }

        // The get-set variable
        internal Model.modelRevitBridge RevitModel
        {
            get
            {
                return _revitModel;
            }

            set
            {
                _revitModel = value;
            }
        }

        // The action function for RetrieveParametersValuesCommand
        private void RetrieveParametersValuesAction()
        {
            if (SelectedWallType != -1)
            {
                ListParameters = new ObservableCollection<string>(RevitModel.GenerateParametersAndValues(SelectedWallType));
            }
        }

        // Constructor
        public ViewmodelRevitBridge()
        {

        }
    }

 

0 Likes