Saturday, December 6, 2014

InjectionMap.Wpf: Extension for InjectionMap

InjectionMap.Wpf is a extension to InjectionMap that allows to define the ViewModel in the xaml of the View. InjectionMap will resolve the ViewModel and bind it to the DataContext of the View. All parameters of the constructor can be resolved using ConstructorInjection.

Create a ViewModel. A class that will be added to the DataContext
namespace ViewModels
{
 public class ViewModel : INotifyPropertyChanged
 {
  public ViewModel(ISettings settings)
  {
    ...
  }
 }
}

Map the dependencies

using(var mapper = new InjectionMapper())
{
      mapper.Map<ISettings, Settings>();
}

Bind the ViewModel to the View

<Window x:Class="MainWindow"
 xmlns:wf="http://schemas.wickedflame.ch/2013/xaml/presentation"
        xmlns:vm="clr-namespace:ViewModels"
        wf:InjectionResolver.Resolve="{x:Type vm:ViewModel}">
 <!-- Optional Namespace for InjectionMap.Wpf: xmlns:wf="clr-namespace:InjectionMap;assembly=personalplaner.common" -->
...
</Window>
The ViewModel will be resolved and bound to the DataContext of the UserControl/Window.

Tuesday, September 2, 2014

InjectionMap.Configuration: Extension for InjectionMap

InjectionMap.Configuration is a small extension to InjectionMap that allows to register the mappings in the application configuration file.


Some simple types to demonstrate the mapping:
public interface IKeyOne { }
public class ObjectTypeOne : IKeyOne { }

public class InjectionMapInitializer : IMapInitializer
{
    public void InitializeMap(IMappingProvider container)
    {
        container.Map<IKeyThree, ObjectTypeThree>();
    }
}

Define the mappings in the config file.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <!-- Define the section -->
    <section name="injectionMap" type="InjectionMap.Configuration.InjectionMapSection, InjectionMap.Configuration" />
  </configSections>

  <injectionMap>
    <mappings>
      <!-- Register Map IKeyOne to ObjectTypeOne -->
      <map key="TestApp.IKeyOne, TestApp" for="TestApp.ObjectTypeOne, TestApp"/>
      <!-- Register Map ObjectTypeOne to self -->
      <map key="TestApp.ObjectTypeOne, TestApp" toSelf="true"/>
    </mappings>
    <initializers>
      <!-- Register MapInitializers -->
      <init type="TestApp.InjectionMapInitializer, TestApp"/>
    </initializers>
  </injectionMap>
</configuration>
</pre>

The Section has to be called "injectionMap" for InjectionMap to find it.
Import the namespace InjectionMap.Configuration and create a instance of InjectionMapper. To initialize the configuration just call the extensionmethod Initialize() on InjectionMapper. This call should only be made once per AppDomain, ideally in the application startup.

using InjectionMap.Configuration;
...

using (var mapper = new InjectionMapper())
{
    mapper.Initialize();
}

InjectionMap.Configuration can be downloaded from NuGet
PM > Install-Package InjectionMap.Configuration

The sourcecode is on Github

Thursday, May 29, 2014

Inject parameters into unmapped Types using InjectionMap

InjectionMap lets you easily resolve mapped types and automatically injects the parameters into the constructor. This helps to resolve types that depend upon constructors that contain parameters.

In this example there there is a type named MappedTyp that has a constructor that accepts a type of ITypeArgument.
public interface ITypeArgument
{
}

public class TypeArgument : ITypeArgument
{
}

public interface IMappedType
{
}

public class MappedType : IMappedType
{
   public UnmappedType(ITypeArgument argument) { }
}

InjectionMap can resolve MappedType when both ITypeArgument and IMappedType are mapped into InjectionMap.
using (var mapper = new InjectionMapper())
{
   mapper.Map<ITypeArgument, TypeArgument>();
   mapper.Map<IMappedType, MappedType>();
}

using (var resolver = new InjectionResolver())
{
   var map = resolver.Resolve<IMappedType>();
}

This will automatically create a instance of MappedTyp and inject a resolved instance of ITypeArgument into the constructor.

Optionaly the parameter can be injected manually. Simply create an instance of UnmappedType and inject TypeArgument by resolving ITypeArgument with InjectionMap.
public interface ITypeArgument
{
}

public class TypeArgument : ITypeArgument
{
}

public class UnmappedType
{
   public UnmappedType(ITypeArgument argument) { }
}

using (var mapper = new InjectionMapper())
{
   mapper.Map<ITypeArgument, TypeArgument>();
}

using (var resolver = new InjectionResolver())
{
   var map = new UnmappedType(resolver.Resolve<ITypeArgument>());
}

Since the version 1.4.3 InjectionMap can also resolve unmapped types and inject the parameters based on the registered maps. Simply create a mapping of ITypeArgument and resolve UnmappedType without mapping it in InjectionMap. InjectionMap creates an instance of UnmappedType and injects TypeArgument by resolving ITypeArgument.

public interface ITypeArgument
{
}

public class TypeArgument : ITypeArgument
{
}

public class UnmappedType
{
   public UnmappedType(ITypeArgument argument) { }
}

using (var mapper = new InjectionMapper())
{
   mapper.Map<ITypeArgument, TypeArgument>();
}

using (var resolver = new InjectionResolver())
{
   var map = resolver.Resolve<UnmappedType>();
}

There is no need to create a new instance of UnmappedType or to register it to the InjectionMap. The Parameters will automatically be resolved and injected.

Wednesday, August 7, 2013

XamlDesigner - BamlExtractor

One grate feature of the XamlDesigner is the ability to open compiled Assemblies that contain Xaml Files and extract the Xaml from the Assembly.

The BamlExtractor displays Styles, Controls (Windows, UserControls, etc.) and ResourceDictionaries.
The Xaml can then be selected for editing or viewing in the XamlDesigner.

This can be a very helpful feature if you want to inspect or create a Style or a Template that is defined in a different Assembly.


There still are several tasks that have to be done but I hope to soon be able to create an alpha Release.

Monday, April 22, 2013

Xaml Designer - The code editor

The wickedflame codeeditor is a small and simple editor for different programing languages written in C# and Xaml (WPF).

The basic features like text manipulation, Undo/Redo and navigation work (should) similar to any other Texteditor. 
The Syntaxhighlighting is defined in a simple xml file. At the moment there is a complete Syntaxhighlighting implementation for Xml/Xaml and a basic implementation for C#.



The Codecompletation is not finished jet. The Completition Window can be shown with items which get filtered as the code gets typed.


I am quite proud of this feature. I was able to extract the Namespaces and load the Assemblies. This way I am able to display all Types that can be contained in the code.
At the moment I only have a Xaml implementation but the C# version should be very easy to add because the basics are the same.

Some features that is still fully missing are:
  • Codeindentation
  • Textsearch / replace
  • Line numbering


Tuesday, April 16, 2013

Xaml Designer

It's been quiet for some time now. But now I finally started a new project an now I am at a state where I can show a little bit.

Xaml Designer

It's a simple editor for xaml with a graphical UI Designer.


I needed a Graphical UI Designer where I could load a *.xaml file and be able to manipulate this. The Project then started as a Test application to see how I could manipulate the XAML. Then I also wanted to see the resulting XAML and be able to manipulate it. So I looked around for a code TextBox. I found a few editors but was not very happy with them so I decided to create a new editor.

I created the Code editor from scratch. There are still some important features missing for a Code edtior like Intelisense (Autocomplete) and the automatic indentation rules.
The Synthaxhighlighting currently only supports C# and XML/XAML. The highlighting Engine is based on xml files. This way the editor can be extended very easily.

The UI Designer was also created from scratch. At the moment it supports selecting an element and Drag & Drop. With this function elements can be moved around and placed in different positions. The elements can be dropped in most Panels and ContentControls. ItemsControl and the remaining Panels still need to be implemented.

At the left side there is the LogicalTree to help selecting elements.

Next I will create a PropertyGrid to be able to change properties without having to edit the XAML.

At the moment I only support WPF but Silverlight is also planned and probably also WinRT.
But at the moment I am far away from any Beta or pre-stable version. I mostly use it in Debug mode and fix the errors on the run.
Hopefuly I will soon have a version ready for release.

Sunday, May 13, 2012

Personalplaner sources are online

I uploaded all sources of the new Personalplaner to personalplaner.codeplex.com.

For development I use a different reopsitory. When I started on the project I didn't expect to ever complete it. So I chose to host it on unfuddle.com.
When I decided to host the setup on codeplex as shareware I also came to the decision to upload the code. Now whenever I create a new stable version I upload the code as well.

I guess it doesn't allways end as expected. Originaly I didn't expect to ever finish the project. And by now I created 14 stable releases that are finished, useable, free and open source :-)

Have fun!