相关文章推荐
焦虑的骆驼  ·  关于Excel vba ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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!

Could you please show me both xaml and cs files of MaterialDesign and mainscreen? Also can I see the way you are displaying the main screen? I only found way to display Window with form.ShowDialog(), but I could not find any way to display Page... – JanSmutný Apr 1, 2022 at 9:43

The accepted solution worked for me. To avoid the dummy code though, I was also able to get MDXT working by adding the following to the code-behind of the resource dictionary:

Assembly.LoadFrom(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "MaterialDesignThemes.Wpf.dll"));
Assembly.LoadFrom(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "MaterialDesignColors.dll"));
                Would you mind showing where exactly you put these in your code-behind and which using-statements were needed? Intellisense isn't helping out very well here.
– fix
                Oct 15, 2021 at 17:52
                @fix using System.IO and System.Reflection. Those lines should go in the constructor in the code-behind, or anywhere where they'll get run before anything tries to display Xaml.
– Trygve
                Oct 21, 2021 at 15:09

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.

Remove the <ResourceDictionary x:Key="123"> element from your ResourceDictionary to begin with:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <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>

You should then be able to set the properties using property element syntax after you have set the Resources property:

<Page ... 
      d:DesignHeight="450" d:DesignWidth="800">
    <Page.Resources>
        <ResourceDictionary Source="/MyAsembly;component/ResourceDictionary/MaterialDesign.xaml" />
    </Page.Resources>
    <Page.Background>
        <DynamicResource ResourceKey="MaterialDesignPaper" />
    </Page.Background>
</Page>
                When i do as the first code snippet says, i still get the error i mentioned in my question: FileNotFoundException: Could not load file or assembly 'MaterialDesignThemes.Wpf, Culture=neutral' or one of its dependencies. . The code breaks before it reaches the second code snippet, i believe.
– Marija Rakic
                Apr 11, 2019 at 12:22
                @MarijaRakic: Then you didn't install the NuGet package properly. You need to reference the MaterialDesignThemes.Wpf assembly.
– mm8
                Apr 11, 2019 at 13:51
                The problem might be the application that i'm building a plugin for (as i mentioned, my project is a class library). I'll investigate further and come back to your suggested solution.
– Marija Rakic
                Apr 11, 2019 at 16:32

Without adding those lines. Double check if the MaterialDesign dll file get copied to the output path of the application.

I have seen such issue before, just adding nonsense code and Visual Studio realize your application that depends on your lib also depends on MaterialDesign lib and then copies the dll again as one would expect in the first place.

Instead of adding those lines you could then

  • Reference MaterialDesign directly in your application as well
  • Use a build event to make sure the DLL is copied to the build path.
  • Those files are definitly present in the debug folder, at the same place where my library is built. Could you clarify steps 1 and 2? If i add using MaterialDesignThemes.Wpf; using MaterialDesignColors;, but never use in code, those include statements will be ignored while compiling the project. – Marija Rakic Apr 29, 2019 at 12:43

    This comment solves the problem for me, but make sure you don't have another errors and if you have just find them and fix theme then try to run the project and it will work.

    using MaterialDesignColors;
    using MaterialDesignThemes.Wpf;
    public MainWindow()
        InitializeMaterialDesign();
        InitializeComponent();
    private void InitializeMaterialDesign()
        // Create dummy objects to force the MaterialDesign assemblies to be loaded
        // from this assembly, which causes the MaterialDesign assemblies to be searched
        // relative to this assembly's path. Otherwise, the MaterialDesign assemblies
        // are searched relative to Eclipse's path, so they're not found.
        var card = new Card();
        var hue = new Hue("Dummy", Colors.Black, Colors.White);
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.