Robert
Robert

Reputation: 2701

WPF, Simple Injector and MaterialDesignThemes Static resource invalid

I have sample app written in WPF and using Simple Injector and Material Design Themes. This is my program file:

private static Container Bootstrap()
{
    // Create the container as usual.
    var container = new Container();

    container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();

    // Register your types, for instance:
    container.Register<IFreewayReviewCreatorDbContext, FreewayReviewCreatorDbContext>(Lifestyle.Scoped);
    container.Register<IUnitOfWorkFactory, UnitOfWorkFactory>(Lifestyle.Transient);
    container.Register<IUnitOfWork, UnitOfWork>(Lifestyle.Scoped);
    container.Register<IReviewBodyBLL, ReviewBodyBLL>(Lifestyle.Transient);

    // Register your windows and view models:
    container.Register<MainWindow>();
    container.Register<MainWindowViewModel>();

    container.Verify();

    return container;
}

private static void RunApplication(Container container)
{
    try
    {
        var app = new App();
        //app.InitializeComponent();
        var mainWindow = container.GetInstance<MainWindow>();
        app.Run(mainWindow);
    }
    catch (Exception ex)
    {
        //Log the exception and exit
    }
}

In the code above view models add registered in Simple Injector.

Now in MainWindow I want to use StaticResource from Material Design. This is my code:

<Window x:Class="FreewayReviewCreator.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:FreewayReviewCreator"
        xmlns:localvm="clr-namespace:FreewayReviewCreator.ViewModel"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        mc:Ignorable="d" Loaded="MainWindow_OnLoaded"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <StackPanel HorizontalAlignment = "Left">

            <TextBox
                    Name="tbxPassword"
                 Text="{Binding Password, Mode = TwoWay}"    
                    HorizontalContentAlignment="Center"                                                        
                    Style="{StaticResource MaterialDesignFloatingHintTextBox}"         
                    MaxLength="28"
                    materialDesign:HintAssist.Hint="Enter your username"    
                            />

Error is in this line: Style="{StaticResource MaterialDesignFloatingHintTextBox}":

System.Windows.Markup.XamlParseException: ''Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '44' and line position '21'.' Exception: Cannot find resource named 'MaterialDesignFloatingHintTextBox'. Resource names are case sensitive.

enter image description here

On this webpage is sample application with StaticResource (I took code from this app):

https://www.c-sharpcorner.com/article/wpf-application-with-googles-material-design/ 

and it works. The only one difference that I can see is that my application has Simple Injector and app from sample doesn't have. References are the same in both of apps:

enter image description here

Upvotes: 0

Views: 1230

Answers (2)

Ric .Net
Ric .Net

Reputation: 5550

Assuming you provide the ResourceDictionaries to the App class as given in the answer of @mm8, you should load and apply the ResourceDictionaries by calling InitializeComponent() in the constructor of the App class.

Like this:

public partial class App : Application
{
    public App()
    {
        this.InitializeComponent();
    }
}

I see in your question that commented this line out. This is probably the result of following the provided startup code from Simple Injector documentation and after this adding the Material Design Themes.

This code is however necessary when you add MergedDictionaries to you App.xaml. So you need to add it back.

Upvotes: 1

mm8
mm8

Reputation: 169400

You should install the MaterialDesignThemes NuGet package and add the following resource dictionaries to your App.xaml file as described in the docs:

<?xml version="1.0" encoding="UTF-8"?>
<Application . . .>
    <Application.Resources>
        <ResourceDictionary>
            <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>
    </Application.Resources>
</Application> 

This has nothing to do with simple injector or whatever IoC container you are using.

You need to import the resources into your app to be able to use them.

Upvotes: 2

Related Questions