WPF, Simple Injector and MaterialDesignThemes Static resource invalid - wpf

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.
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:

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.

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.

Related

Get ResourceDictionary source from static member with design-time support

My basic goal is to have a ResourceDictionary in a dll which I can use in another WPF project via ResourceDictionary.MergedDictionaries. But I don't want to reference the ResourceDictionary by hard-coding the URI in the XAML of the referencing application, I want to instead reference some static member which will provide the URI.
I have some simplified code which is "working", but only at runtime. At design-time it throws errors and I get no IntelliSense support. For this simplified example, everything is in one assembly (no separate dll).
Dic.xaml (the resource dictionary I want to reference):
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush Color="Blue" x:Key="BlueBrush"/>
</ResourceDictionary>
Foo (the module to hold the static member with the URI):
(VB.NET version)
Public Module Foo
'[VBTest] is the name of assembly
Public ReadOnly Property URI As New Uri("pack://application:,,,/VBTest;component/Dic.xaml")
End Module
(C# version)
public static class Foo
{
//[VBTest] is the name of assembly
public static Uri URI { get; } = new Uri("pack://application:,,,/VBTest;component/Dic.xaml");
}
And then finally, the place in the application where I want to reference the ResourceDictionary:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:VBTest">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="{x:Static local:Foo.URI}"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Border Width="100" Height="100" Background="{StaticResource BlueBrush}"/>
</Window>
At design-time, I get two errors:
XDG0062 An error occurred while finding the resource dictionary "".
XDG0062 The resource "BlueBrush" could not be resolved.
However, the project will build and run just fine. And will show the intended blue square.
The question is, how can I get this to work at design-time?
I found a thankfully easy workaround. Maybe not the prettiest, but it is elegant. I took inspiration from this answer to a somewhat related question.
By creating my own class inheriting from ResourceDictionary, I can better control the loading behavior. That sounds like it would be complicated, but really all I had to do was set Source as part of the constructor and everything just worked.
The below is added to the code file (outsite of Module/static class Foo):
Public Class StylesResourceDictionary
Inherits ResourceDictionary
Public Sub New()
MyBase.New()
Source = URI
End Sub
End Class
Note that Source = URI is referencing Foo.URI from the question code.
And then the XAML file becomes:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:VBTest">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<local:StylesResourceDictionary/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Border Width="100" Height="100" Background="{StaticResource BlueBrush}"/>
</Window>
And bam, full design-time support without hard-coding the URI into the referencing application. Control of the ResourceDictionary and its URI is now in the domain of the dll, and can be referenced in a (kinda) static fashion using the dedicated class.

How to reference an app resource in a xaml Window?

I've added a reference to an app resource in my View's window. But when I declare the resource I get the error:
Error 10 RegexValidationRule is not supported in a Windows Presentation Foundation (WPF) project.
This is how I added the resource to the View:
<Window.Resources>
<validators:RegexValidationRule x:Key="localRegexValidationRule"/>
</Window.Resources>
Does anyone know how to reference the resource correctly in WPF?
I've followed this solution but still get the same error on the resource. This is how I've declared the resource in app.xaml within a resource dictionary:
<Application x:Class="MongoDBApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:MongoDBApp.Converters"
xmlns:validators="clr-namespace:MongoDBApp.Validators"
StartupUri="pack://application:,,,/Views/MainView.xaml">
<Application.Resources>
<ResourceDictionary>
<converters:BoolToNonVisibilityConverter x:Key="localBoolToNonVisibilityConverter" />
<converters:BoolToVisibilityConverter x:Key="localBoolToVisibilityConverter" />
<validators:RegexValidationRule x:Key="localRegexValidationRule" />
</ResourceDictionary>
</Application.Resources>
</Application>
Have you added the appropriate using statement in your View file? It should have the same statement as your App.xaml file:
xmlns:validators="clr-namespace:MongoDBApp.Validators"

Define a string as a static resource

IS there a way to define a constant string to be used as a static resource across the whole application?
I am running a Wpf application but there is no main xaml form. The application is a collection of xaml controls handled by a single classic .cs form.
You can define it as an application resource:
<Application x:Class="xxxxxx"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:clr="clr-namespace:System;assembly=mscorlib"
StartupUri="MainWindow.xaml">
<Application.Resources>
<clr:String x:Key="MyConstString">My string</clr:String>
</Application.Resources>
</Application>
Supplemantary to the answer by #FelicePollano - for code indentation to work I put this as a separate 'answer'.
If you happen to have your original constant defined in a .cs-file you can avoid duplicating its value in <Application.Resources> by this:
<x:Static x:Key="MyConstString" Member="local:Constants.MyString" />
For the reference local above to work you need to include the namespace xmlns:local="clr-namespace:Utils" in the tag <Application>.
The cs-class could then look like this:
namespace Utils
{
public class Constants
{
public const string MyString = "My string";
}
}
An example on usage in the xaml-code could then be:
<TextBlock Text="{StaticResource MyConstString}" />
Just add a resource dictionary XAML file, let's say it's named Dictionary.xaml (Visual Studio can create you one automatically)
Then, add your static resource in this dictionary.
To finish, reference the dictionary in all your XAML controls:
<UserControl.Resources>
<ResourceDictionary Source="Dictionary.xaml"/>
</UserControl.Resources>
You can use like this:
First, sample constant variable:
namespace Constants
{
public class ControlNames
{
public const string WrapperGridName = "WrapperGrid";
}
}
And second XAML using:
<TextBlock Text="{x:Static Member=Constants:ControlNames.WrapperGridName}"

Designer rejecting DataTemplate.DataType

I am try to fit some WPF into my current Windows Forms application. When I use this simple user control, the designer for that control does not reload.
This only happens in this application. If I make a clean Windows Forms project, add these files, the designer works fine.
I have tried a reload of Visual Studio, and cleans / rebuilds of the application.
Any ideas? (These are for the items in a ListBox, so x:Key is not an option.)
P.S. How do I get rid of all those trailing blank lines in my code listing?
DETAILS:
MyClasses.cs
namespace MyNamespace {
internal class MyClassInternal {}
public class MyClassPublic {}
}
MyUserControl.xaml
<UserControl x:Class="MyNamespace.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyNamespace"
Height="300" Width="300">
<UserControl.Resources>
<DataTemplate DataType="{x:Type local:MyClassInternal}"/> <!--OK-->
<ObjectDataProvider x:Key="ClassPublicKey" ObjectType="{x:Type local:MyClassPublic}"/> <!--OK-->
<!-- Type reference cannot find public type named 'MyClassPublic' -->
<DataTemplate DataType="{x:Type local:MyClassPublic}"/> <!--FAILS-->
</UserControl.Resources>
<TextBlock>Hello World</TextBlock>
</UserControl>
MyUserControl.xaml.cs
using System.Windows.Controls;
namespace MyNamespace {
public partial class MyUserControl :UserControl {
public MyUserControl() {
InitializeComponent();
}
}
}
It was caused by having a space in the Assembly name.

Testing a WPF Window with StaticResources

I have a simple Window with a reference to a StaticResource in the App.xaml.
App.xaml resource definition:
<!-- Standard Text Box Style -->
<Style x:Key="textBoxStyleStd" TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="14" />
</Style>
Window componets using the resource:
<TextBlock Grid.Column="1" Grid.Row="0" Name="stationIdTitle"
Style="{StaticResource textBlockStyleStd}"
VerticalAlignment="Center" HorizontalAlignment="Center"
Text="{LocText Key=Title, Dict={StaticResource Dictionary},
Assembly={StaticResource Assembly}}"/>
When trying to unit test this Window I get the error:
System.Windows.Markup.XamlParseException: Cannot find resource named
'{textBlockStyleStd}'. Resource names are case sensitive. Error at
object 'stationIdTitle' in markup file
'Zpg;component/guicomponenets/screens/enterstationidscreen.xaml' Line
23 Position 71.
Is there any way around this? My unit test code is:
[Test]
public void TestEnterKeyPressedNoText()
{
IPickingBusinessObject pickingBusinessObject = mock.StrictMock<IPickingBusinessObject>();
EnterStationIdScreen objectUnderTest = new EnterStationIdScreen(pickingBusinessObject);
Assert.AreEqual(Visibility.Visible, objectUnderTest.stationIdError.Visibility);
Assert.AreEqual("werwe", "oksdf");
Replay();
objectUnderTest.EnterKeyPressed();
Verify();
}
Thanks Kent,
I looked at your suggestions and in most scenarios I agree models should be used and tested however, there is some code associated with the controls (e.g. TextBox visibility) I still wanted to test. To get round this you can create an instance of your Application (but not initialize it) and add the resources manually. This does lead to duplication in the App.xaml and the base unit test but this allows me to complete the tests I wanted.
if (Application.Current == null)
{
App application = new App();
#region Add Static Resources from the App.xaml
Style textBoxStyle = new Style(typeof(TextBox));
textBoxStyle.Setters.Add(new Setter(TextBox.FontSizeProperty, 14d));
Style textBlockStyle = new Style(typeof(TextBlock));
textBlockStyle.Setters.Add(new Setter(TextBlock.FontSizeProperty, 14d));
application.Resources.Add("TextBoxStyleStd", textBoxStyle);
application.Resources.Add("TextBlockStyleStd", textBlockStyle);
application.Resources.Add("TextBlockStyleError", textBlockStyle);
application.Resources.Add("Assembly", "Zpg");
#endregion
}
In the context of your unit test, there is no WPF application running. Therefore, the Window won't find the resource.
My way around this would be to not unit test your views. Instead, use MVVM and unit test your view models. If you want to test your views, write integration tests instead. Your integration tests can actually kick off the application and therefore much more closely imitate the real running of your app.
When I use fully qualified assembly names in my app.xaml resource entries, I only need to instanciate the App() class. In this example, all resources lies in the Majesty_of_Omega_GUI assembly, which is referred by the UnitTest.DLL
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Majesty_of_Omega.GUI.App"
StartupUri="Pages/MainPage.xaml"
>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Majesty_of_Omega_GUI;component/Resources/MainScreens.xaml" />
<ResourceDictionary Source="pack://application:,,,/Majesty_of_Omega_GUI;component/Resources/PanelResources.xaml" />
<ResourceDictionary Source="pack://application:,,,/Majesty_of_Omega_GUI;component/Simple Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Test function:
[Test]
public void Testfunction()
{
if (Application.Current == null)
{
App application = new App();
}
SomePage page = new SomePage();
Actually, you can use the same Application and if the resources are from the same assembly, you've got to call the InitializeComponents methods to make it works (Source here).
Have a nice day !

Resources