在开发Revit插件的时候很多会使用WPF创建窗口,我这里引用了MaterialDesignThemes这个组件,在这个文章里面记录一下相应的步骤和问题

使用

安装

  1. 直接从nuget中搜索MaterialDesignThemes安装
![在这里插入图片描述](https://cdn.bimath.com/blog/pg/Snipaste_2026-01-04_16-55-22.png)
2. 添加reosurces
1
2
3
4
5
6
7
8
9
10
11
12
<Window.Resources>
<ResourceDictionary>
<viewmodel:ObjectConvert x:Key="ObjectConverter" ></viewmodel:ObjectConvert>
<ResourceDictionary.MergedDictionaries>
<materialDesign:BundledTheme
BaseTheme="Light"
PrimaryColor="DeepPurple"
SecondaryColor="Lime" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
## 问题 如果用的是多版本自动适配的框架而引用的是最新版本的`4.60`的组件,即使实在高framework版本里面适配,也会报错 `xamlParseException ---- DllNotFound`,此时将组件的包修改为`4.5.0`适配到2016版本的framework即可解决

运行

如果使用AddinManager直接调试,可能发现不了问题,所有的组件会正常运行,当我们使用addin文件正式添加的时候,会报错xamlParseException ---- DllNotFound, 此处有j几个解决方案

[] Revit二次开发之Could not load file or assembly
[x] WPF,Could not load file or assembly(无法加载文件或者程序集)
[] https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/issues/427
[] https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/issues/427

上面有四篇文章,骑士的办法是初始化material组件里面的类,强制程序检索路径下面的dll从而载入文件,但是再Revit里面还是失败,第三篇,第四篇则是类似问题,开源作者的回复,可以参照一下。

解决办法

如果使用MVVM模式的话可以在StartUp类中那倒MainViewModel并引用materialTheme.wpfdll文件,统一将之前遇到问题的dll文件一起load

1
AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public static Assembly AssemblyResolve(object sender, ResolveEventArgs args)
{
var assName = new AssemblyName(args.Name).FullName;
try
{
if (assName.Contains("WPF_CaptureShot") && !assName.Contains("resources"))
{
string file = Path.GetDirectoryName(typeof(MakeDataRevitCommand).Assembly.Location) + "\\" + assName.Split(',')[0] + ".dll";

byte[] buff = System.IO.File.ReadAllBytes(file);
var da = Assembly.Load(buff);
return da;
}
else if (assName.Contains("MaterialDesignThemes.Wpf") && !assName.Contains("resources"))
{
string pathLoc = Assembly.GetExecutingAssembly().Location;
FileInfo finfo = new FileInfo(pathLoc);
var pathDir = finfo.DirectoryName;
var load = Assembly.LoadFrom($"{pathDir}\\MaterialDesignThemes.Wpf.dll");
Assembly.LoadFrom($"{pathDir}\\MaterialDesignColors.dll");
return load;
}
else if (assName.Contains(".resources"))
{

return null;
}
else
{
throw new DllNotFoundException("BIMCooperative" + assName);
}

}
catch (Exception ex)
{
throw new DllNotFoundException(assName);//否则抛出加载失败的异常
}
}