Category Archives: C#

Enter C# Unity Container, Inversion of Control

Coding in C# is pretty straight forward although sometimes you see errors every now and then. Working with WPF(Window Presentation Foundation) and MVVM can get really complex with large projects you can get lost in the sea of codes.

One thing Im going to talk about is Dependency, we use it all the time. Sometimes we need an model object that represent the data in our class and consume it, sometimes we need a database layer class to every object that will communicate with our server.

Inversion of Control is a pattern on how to pass dependency object through constructor. Enter Unity Container, its a Microsoft object that handles dependencies through the use of IoC.

We create the instance of the UnityContainer object and register types so unity will construct our dependency for us. You then resolve the classes that are involved in the dependency by calling obj.Resolve<typeof>(). This will chain up the classes with their dependency object injected.

Check links below for a simple explanation of the architecture.

https://www.youtube.com/watch?v=CWwe9Z0Gyew PART 1
https://www.youtube.com/watch?v=PsIbevgzQQE PART 2

Credit to the author

C#: WPF DependencyObject; What is it exactly?

Been developing a Rename Tool in C# using WPF as my window form, and data binding is giving me a bit of trouble understanding. There are other ways to make data binding in WPF, and Dependency Property is the one that boggles the mind.

Alas I have found an article that led me to a more thorough explanation to why you would use it.

http://www.pinfaq.com/32/what-is-dependency-object-in-wpf-where-should-i-use-it

In short Dependency Object is the object where WPF controls derive from and it provides all the validation, notification, updating , callbacks otherwise you would implement yourself which is possible but just time consuming.

This comes down to using Dependency Property  on your own class.

Adding Custom Context menu in Windows and my Rename Tool

I have been thinking of putting a custom context menu entry in my program, and this is the registry place to put your values into.

How to Add Any Application Shortcut to Windows Explorer’s Context Menu

And all you need to do is code your application to add them in your Visual Studio set up install function using the RegistryKey class in C# (at least), or use Inno Installer program to add a section as [Registry], the Inno Setup help is available if you need a reference.

I am thinking of creating a program that lets you rename all the filenames in a folder by adding a prefix or suffix or using regular expressions by right clicking the folder and call upon the program and a windows pops-up confirming your intention to rename.

Its very handy little tool , Im just in the process of putting them together. So the step would be:

  • Make the program
  • Create an installer for the program most likely use Inno Installer
  • Let the installer create the registry entry for you using { yourprogram.exe 1% } where the 1% is the folder path.
  • And run your program base on the folder path otherwise the user can select the folder to rename files.

 

Creating a C# application set up project with custom action

I have been looking to get  my C# application in Visual Studio to be packaged for easy installation.

I found a really good article that explains the steps and other custom action to be included in the installation set up as well.

It also bundles up the .NET Framework of your choosing as you install the program. Mainly Im trying to run a code that injects registry entry to the register while install is in process. As there are tons you can do to added functionality to your application using the registry in Windows.

Check this link out: http://www.c-sharpcorner.com/UploadFile/1492b1/creating-an-msi-package-for-C-Sharp-windows-application-using-a-v/

Credit to the Author of course I will look into this article for reference whenever I need to bundle up my application.

Important note: The Visual Studio Set up project isnt included in VS2012 and up, as they have a different installer used, you might need to download the old installer project at this location:

https://marketplace.visualstudio.com/items?itemName=UnniRavindranathan-MSFT.MicrosoftVisualStudio2013InstallerProjects