I am creating a program using WPF by mvvm method and I use Revit API classes on some methods and properties. The first time I reference RevitAPI and RevitAPIUI dlls does not seem to create a problem with the WPF window, but when I use a view model (which contains properties declared with Revit classes) to be the datacontext of my WPF window, an error is generated (btw, the error is some sort of "Cannot find RevitAPI Version=2015.0.0.0 Culture=neutral, PublicKeyToken=null or one of its dependencies").
Although the programs work normally at runtime, the regeneration of WPF window does not work properly especially after compiling, and so I cannot edit or modify them properly too unless I reload it on my own.
Is there any workaround to avoid this WPF window regeneration problem involving Revit API dlls? Or is there something that I missed?
Solved! Go to Solution.
Solved by jeremytammik. Go to Solution.
Solved by homer_anave. Go to Solution.
Hi
If I understood correctly, you are having problems while editing your WPF window / project using Visual Studio as you said there's no problems during runtime. If that's the case then the reason might be following kind.
When you are running Revit and some of your add-ins, it must be noted that Revit API assemblies are located within the Revit program file folder, which in most cases is different than the folder where the add-in assemblies are. The reason why everything works is that Revit application takes care of setting environment so that your add-in finds the needed dependencies during the runtime. But when using visual studio editor and especially XAML editor which shows the resulting window during design time, there's no such a environment settings applied to find Revit modules correctly...or at least this is my guess.
You could try to solve this issue by placing revit API assemblies into your project's output directory...I mean the folder where the resulting assembly will appear.
Ah...one more thing. You said that you are setting some of the Revit API classes as a DataContext in your WPF window. I would say that sounds a bit awkward solution because usually you would consider Revit API classes as a model. So preferably you would wrap those into some VM class instead. Of course sometimes it might be convenient just to use API classes directly.
Did you set 'Copy Local' to false on the Revit API assemblies?
http://thebuildingcoder.typepad.com/blog/2011/08/set-copy-local-to-false.html
Cheers,
Jeremy
Thanks for the replies.
The application has the Revit API dlls' "Copy to Local" set to false by default. But with some tweaking, "RevitAPIUI.dll" is said to be missing now.
I tried the "Copy to Local" set to True, but it just even made things worse.
I checked the other (lighter) applications (because this problematic one is huge), but there are no errors of this kind regenerated.
Well, after a very long time integrating WPF in my own programs, my recommendation is NOT to use any Revit API class inside the view model class where you assign to the WPF window's DataContext.
If ever you want to pass or get any information coming from an element or a parameter, it is better to extract its Id's IntegerValue, and when you are done with the WPF window, just create ElementIds from the integer values you acquired from the WPF window.
There may be other solutions out there but this is the solution I have so far.
Thank you very much!
Thank you very much for sharing this important nugget of experience!
I added it to an existing discussion by The Building Coder:
http://thebuildingcoder.typepad.com/blog/2016/01/devday-conference-in-munich-and-wpf-doevents.html#5
Best regards,
Jeremy
This works for me:
public class ElementIdConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is R.ElementId) { return (value as ElementId).IntegerValue; } return -1; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { if (value is string) { int id; if (int.TryParse(value as string, out id)) { return new ElementId(id); } } return ElementId.InvalidElementId; } }
In this case, I expose a ElementId property in the view model.
You can also add validation to give some feedback to the end user.
Muito obrigado!
http://thebuildingcoder.typepad.com/blog/2016/01/devday-conference-in-munich-and-wpf-doevents.html#6
Cheers,
Jeremy
Can't find what you're looking for? Ask the community or share your knowledge.