Revit addin FileNotFoundException

Revit addin FileNotFoundException

joseluisps9403
Explorer Explorer
4,867 Views
9 Replies
Message 1 of 10

Revit addin FileNotFoundException

joseluisps9403
Explorer
Explorer

Hello everyone, I'm developing an adding for Revit and have several buttons with GUI I decided to create a new project to put all the resource dictionaries inside. When I compile the app the compiler is not throwing me any error and even the Visual Studio Designer is applying the styles correctly. But when I open Revit and try to run a button with GUI is throwing back a FileNotFoundException and says that can locate my styles .dll.

 

System.Windows.Markup.XamlParseException: ''Set property 'System.Windows.ResourceDictionary.Source' threw an exception.' Line number '17' and line position '18'.'

 

Inner Exception

FileNotFoundException: Could not load file or assembly 'Themes.SharedStyles, Culture=neutral' or one of its dependencies. The system cannot find the file specified.

 

This is the error, my 2 .dll are copied to addins directory.

joseluisps9403_0-1711015535844.png

This is how I importing the styles into the MainWindow.xaml File

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Themes.SharedStyles;component/ButtonStyles/TopBarButton.xaml"/>
            <ResourceDictionary Source="pack://application:,,,/Themes.SharedStyles;component/ButtonStyles/MainButton.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

 

And this is my file organization

joseluisps9403_1-1711015693713.png

Anyone have an idea what is happening I checked all my compilation configuration and everything seems to be fine.

I would appreciate I somebody could help me, thanks.

0 Likes
Accepted solutions (1)
4,868 Views
9 Replies
Replies (9)
Message 2 of 10

vitruviusbim
Participant
Participant
 private static void LoadLibExtensions()
  {
 

      var libraryPaths = new List<string>
      {
          "Microsoft.AspNetCore.Http.dll", // Your dll
          "MaterialDesignThemes.Wpf.dll",
          "MaterialDesignColors.dll",
       };


      foreach (var libraryPath in libraryPaths)
      {
          string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
          string xmlFileName = Path.Combine(assemblyFolder, libraryPath);
          // Update with the correct path
          Assembly externalAssembly = Assembly.LoadFrom(xmlFileName);
      }
  }

 

 

1.Add this  method to the  ExternalCommand  .  2.Change  names in libraryPaths   to your  libarary name  

Message 3 of 10

Moustafa_K
Collaborator
Collaborator

you need to do some several checks, just out of the top of my head:

 

  1. the assembly file is in the addin folder, where you initially loading your addin
  2. the path for button styles or any address after the common/**** .xaml  is correct and exists
  3. if there are any icons that is not also in correct address
  4. it may occur that the reactor doesn't find the assembly, you can try assembly resolver, see this post as a possible solution
  5. if there is a specific version you can also add that to the source:

 

pack://application:,,,/{assemblyname without dll};v{the assembly version};component/{address to xaml file}"

 

Moustafa Khalil
Cropped-Sharp-Bim-500x125-Autodesk-1
Message 4 of 10

joseluisps9403
Explorer
Explorer
Accepted solution

I found the solution basically you need to add this line at the beginning of the constructor.

 

Assembly.ResourceAssembly = Assembly.GetExecutingAssembly();

 

 This will route the resource dictionaries to the xaml file.

Thanks for the help anyways

0 Likes
Message 5 of 10

joseluisps9403
Explorer
Explorer
This doesn't solve the issue. The application needs to load the resources when starts usually when you work on normal applications for windows using WPF this will be handle by app.xaml. For Revit Addings the structure is different because the application entry point, so the solution is load the resource dictionaries at the before the GUI gets loaded.
0 Likes
Message 6 of 10

joseluisps9403
Explorer
Explorer
That's great but I think this is for when you compile your GUI resources separated from the Revit adding dll. In my case everything is in the same dll, is a small app. But anyways I gonna save it for future use thanks.
0 Likes
Message 7 of 10

TheLaymansShaman
Explorer
Explorer

Hi I am having a similar problem over here can you just confirm in which class you are adding this line of code. I'm still learning and need a bit more detail about where to add that thanks!

0 Likes
Message 8 of 10

TheLaymansShaman
Explorer
Explorer

Hi can you be a bit more specific about where you added this line of code please I have a similar problem and not 100% sure where to put it thanks !

0 Likes
Message 9 of 10

nice3point
Advocate
Advocate

It is bad practice to use Assembly force loading, this should be handled by the .Net dependency resolution mechanism.

 

Subscribe to the AppDomain.CurrentDomain.AssemblyResolve event, don't use Assembly.LoadFrom in its pure form, it will result in a strong hardcode and you will still not be able to hook related dependencies and get problems in some libraries.

 

My suggestion: AppDomain.CurrentDomain.AssemblyResolve or RevitToolkit nuget package

0 Likes
Message 10 of 10

jose_marquez5Z5GC
Explorer
Explorer

Almost one year after you saved my life, lol. In this case I was trying to use Mater Design and looking for answers I found my old question and your answer solve my issue, thanks again.

0 Likes