Revit 2018 Addin - After initializing a singleton at startup, assigning the instance to a variable in a command keeps the variable as null
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
7Developing a multi-version Revit addin. My code is running fine on Revit 2019 and above, but is having an issue in Revit 2018.
I am initializing a singleton class (HostApplication) at startup to gather common properties that can be used in all commands.
Stepping into the code, the singleton is being initialized successfully at startup, but when I try to assign it's instance to a variable in command level, the variable remains null.
Stepping into the command, I can see that the instance is still populated, it is just not being accessed by the = operator?
As you can see in the screenshot above, the debugger is showing that HostApplication.Instance has a value, but the ha variable is still null after the assignment (I made the snapshot while I was trying to assign the variable again as a desperate measure).
As far as I can see, this is not a Revit Api issue, could it be related to the underlaying .net framework being used in Revit 2018?
Here is the HostApplication class:
public class HostApplication { public static void Init(UIControlledApplication uiApp, Assembly bimotoAssembly) { if (Instance == null) Instance = new HostApplication(uiApp, bimotoAssembly); } private readonly UIControlledApplication _uiApp; private readonly Assembly _bimotoAssembly; public static HostApplication Instance { get; private set; } = null; private HostApplication(UIControlledApplication uiApp, Assembly bimotoAssembly) { _uiApp = uiApp; _bimotoAssembly = bimotoAssembly; BimotoInstallationDirectory = (new DirectoryInfo(_bimotoAssembly.Location)).Parent; BimotoSharedFolderPath = #if DEBUG "C:\\Dev\\Repos\\PAE.BIMOTO\\src\\PAE.BIMOTO.Revit.Ribbon\\Shared"; #else BimotoInstallationDirectory.Parent.FullName + "\\Shared"; #endif BimotoRevitUsersSettings = BimotoRevitUserSettings.ReadFromFile( $"{BimotoSharedFolderPath}\\{BimotoRevitKeys.UserSettingsFileName}.json"); } public static int DefaultDpi = 96; public ControlledApplication App => _uiApp.ControlledApplication; public int Version => int.Parse(App.VersionNumber); public string BimotoVersion => _bimotoAssembly.GetName().Version.ToString(); public DirectoryInfo BimotoInstallationDirectory { get; private set; } public string BimotoSharedFolderPath { get; private set; } public BimotoRevitUserSettings BimotoRevitUsersSettings { get; private set; } public IntPtr AppWindowHandle { get { #if REVIT2018 return Autodesk.Windows.ComponentManager.ApplicationWindow; #else return _uiApp.MainWindowHandle; #endif } } public System.Windows.Forms.Screen AppScreen => System.Windows.Forms.Screen.FromHandle(AppWindowHandle); public double ScreenScaleFactor { get { if (AppScreen == null) return 1; var actualWidth = SystemParameters.PrimaryScreenWidth; var scaledWidth = Screen.PrimaryScreen.WorkingArea.Width; return Math.Abs(scaledWidth / actualWidth); } } }
Initializing it at startup:
public Result OnStartup(UIControlledApplication application) { //Get the path of the Ribbon dll MainAssembly = Assembly.GetExecutingAssembly(); AssemblyPath = MainAssembly.Location; //Initialize the application wrapper HostApplication.Init(application, MainAssembly); ... ... }
Accessing it's instance in the command:
//get defaults file WarehouseBrowserUserSettings warehouseSettings = null; try { var ha = HostApplication.Instance; //in Revit 2018 ha remains null after the assignment var us = ha.BimotoRevitUsersSettings; warehouseSettings = us.GetSetting<WarehouseBrowserUserSettings>(); } catch (Exception e) { TaskDialog.Show("Error", e.Message);