
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
Solved! Go to Solution.