Revit secondary development uses MaterialDesignThemes.Wpf

When developing Revit plug-ins, many people will use WPF to create windows. I quoted MaterialDesignThemesthis component here and recorded the corresponding steps and problems in this article.

use

Install

  1. MaterialDesignThemesSearch and install directly from nuget
    Insert image description here
  2. Add reosurces
<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>

question

If you are using a multi-version automatic adaptation framework and refer to the latest version of 4.60a component, an error will be reported even if it is adapted to a higher framework version.
xamlParseException ---- DllNotFoundIn this case, modify the component package to 4.5.0adapt to the 2016 version of the framework. solvable

run

If you use AddinManager to debug directly, you may not be able to find the problem. All components will run normally. When we use the addin file to formally add, an error will be reported xamlParseException ---- DllNotFound. There are several solutions here.

[] Revit secondary development Could not load file or assembly
[x] WPF, Could not load file or assembly (Cannot load file or assembly)
[] https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/issues/427
[ ] https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/issues/427

There are four articles above. The knight's method is to initialize the class in the material component and force the program to retrieve the dll under the path to load the file, but it still fails in Revit. The third and fourth articles have similar problems. Open source author Please refer to the reply.

Solution

If you use the MVVM mode, you can invert the MainViewModel in the StartUp class and reference the materialTheme.wpfdll file, and load the dll files that encountered problems before.

AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve;
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);//否则抛出加载失败的异常
            }
        }

Guess you like

Origin blog.csdn.net/qq_41059339/article/details/127464567