Search code examples
c#wpfmvvmrevitmaterial-design-in-xaml

How to include MaterialDesignXamlToolkit to WPF class library?


I'm trying to use MaterialDesignXamlToolkit in my WPF class library (.NET framework). I'm following their official quick start tutorial, but since i do not have App.xaml, i had to make some adjustments. Apperently some step was wrong, but i do not know which one.

1) I installed MaterialDesignXamlToolkit using Nuget.

2) I created ResourceDictionary with the following code: (i specified the key because there is an error if i don't)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary x:Key="123">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</ResourceDictionary>

If i remove <ResourceDictionary x:Key="123"> element, then i get an error:

System.Windows.Markup.XamlParseException: Set property 'System.Windows.ResourceDictionary.Source' threw an exception.

FileNotFoundException: Could not load file or assembly 'MaterialDesignThemes.Wpf, Culture=neutral' or one of its dependencies.

3) My 'main screen' is Page, so i added the resource to it:

    <Page.Resources>
        <ResourceDictionary Source="/MyAsembly;component/ResourceDictionary/MaterialDesign.xaml" />
    </Page.Resources>

4) The obvious problem occurs here (this is the second step of the official tutorial): i add the following code to my Page:

<Page ...
      xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
      TextElement.Foreground="{DynamicResource MaterialDesignBody}"
      TextElement.FontWeight="Regular"
      TextElement.FontSize="13"
      TextOptions.TextFormattingMode="Ideal"
      TextOptions.TextRenderingMode="Auto"
      Background="{DynamicResource MaterialDesignPaper}"
      FontFamily="{DynamicResource MaterialDesignFont}">

But i get a warning that: The resource {MaterialDesignBody, MaterialDesignPaper, MaterialDesignFont} could not be resolved.

Some of the solutions i tried pointed out that the ResourceDictionary's build action should be page, and it is.

Any help would be greatly appreciated!


Solution

  • Now that i've solved the problem, i realize one important information is missing from my question: i was following MVVM pattern (so all my code behind files were empty).

    The problem was with the way Revit (the application that i was building a plugin for) loads libraries that a plugin is using. I still do not understand the internal logic of it, but the following two lines added to the code behind of the first page what is being loaded solved the problem for me:

    ColorZoneAssist.SetMode(new GroupBox(), ColorZoneMode.Accent);
    Hue hue = new Hue("name", System.Windows.Media.Color.FromArgb(1, 2, 3, 4), System.Windows.Media.Color.FromArgb(1, 5, 6, 7));
    

    I cannot stress enought that those two lines of code are a complite bullshit (since i do not want to place any logic to code behind), but the libraries won't otherwise be loaded. This code somehow 'forces' Revit to load Material design libraries (1st line of code uses MaterialDesignTheme.Wpf, and the 2nd MaterialDesignColors), since (i assume) it can already tell at compile time that those libraries are needed.