0

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

Steven
  • 166,672
  • 24
  • 332
  • 435
Robert
  • 2,571
  • 10
  • 63
  • 95
  • did you perform this step: `Add some lines of code under tag for setting up the default template into App.xaml`? DI container is irrelevant for xaml – ASh Dec 16 '19 at 14:35
  • Did you install MaterialDesign into your app and include the resource dictionaries in your App.xaml as described under "getting started" [here](http://materialdesigninxaml.net/)? How is this related to simple injector? – mm8 Dec 16 '19 at 14:35
  • Yes I had install MaterialDesign and I had added ResourceDictionary to my App.xaml. I didn't show it only in my post – Robert Dec 16 '19 at 21:29

2 Answers2

2

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.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • I've installed MaterialDesignThemes and above ResourcesDictionary was added to my App.xaml and despite of this I had the error – Robert Dec 16 '19 at 21:27
1

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.

Ric .Net
  • 5,540
  • 1
  • 20
  • 39
  • This is WPF and NET. Framework project. There is no InitializeComponent() method in Application class. This method is only in Window class. – Robert Dec 18 '19 at 07:05
  • 1
    Problem was with namespace and it was a reason why I didn't see InitializeComponent. This is solution: https://stackoverflow.com/questions/6925584/the-name-initializecomponent-does-not-exist-in-the-current-context. But your sugestion was good. – Robert Dec 18 '19 at 07:57
  • I don't think this has anything to do with Material Design specifically or even with namespaces. The issue is caused by the window requiring resources that will only be loaded in App.xaml. This is a problem because the call to `container.Verify()` in the `Bootstrap()` method instantiates all registered classes once just to see if they'd throw exceptions. In this case they will because `App.InitializeComponent()` has not yet been called at that point and thus the global resources are not yet available. Could you maybe elaborate on how you resolved this as I myself haven't figured it out yet... – Oliver Giesen Apr 03 '21 at 23:40
  • Also see https://github.com/simpleinjector/SimpleInjector/issues/493 - However, while the application now runs fine with the `app.InitializeComponent()` in place I then get the new problem that the process no longer terminates after closing the main window... – Oliver Giesen Apr 04 '21 at 00:50