Changing the default class template in Visual Studio 2012/2013

I moved to Visual Studio 2013 when it came out and have not had to go back to the 2012 version for some time now. That is a sign of how good the new version is. It does however mean this article now needs an update (again).

To recap:

How many times have you hit SHIFT+CTRL+A to get a new class in a Visual Studio 2012/2013 project? 100s? 1000s?

And after, that how many times do you have to modify the class file it creates because it is not exactly what you wanted… For me: Every single time!

For starters I want the class public by default. I also want standard regions installed to save me adding them afterwards.

Here’s how to do it:

1. Find the files

The files have not moved around much so are currently in C:\<Program Files>\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplates\CSharp\Code\1033

(in VS2012 they are in C:\<Program Files>\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Code\1033)

The file you want is in an appropriately named folder (no more ZIP files!). If you open the Class folder you will find the following 2 files:

  • Class.cs
  • Class.vstemplate

2. Backup the original files!

Very important step as things do go wrong. Backup the original files e.g. by copying it to Class.cs. Orig so that it is not recognised by Visual Studio.

3. Change the Class.cs template file

This is where you get to be creative. You will notice the original looks something like this:

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;

namespace $rootnamespace$
{
	class $safeitemrootname$
	{
	}
}

You might want something more like this:

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;

namespace $rootnamespace$
{
	public class $safeitemrootname$
	{
		#region Constants
		#endregion Constants

		#region Fields
		#endregion Fields

		#region Properties
		#endregion Properties

		#region Constructors
		#endregion Constructors

		#region Public methods
		#endregion Public methods

		#region Class override methods
		#endregion Class override methods

		#region Class extensions - virtual methods
		#endregion Class extensions - virtual methods

		#region Private helper methods
		#endregion Private helper methods
	}
}

You will notice a number of Macros $ commands in the template. The complete list is here.

4. Save your changes
Simply save your edited files (again, there is no longer a ZIP file to recompress with VS 2012).

5. Tell Visual Studio about the changes
Your new changes will not be loaded unless you explicitly tell Visual Studio to reload all templates.

  • Close Visual Studio (or the change swill not show until next time you run it)
  • Open a command prompt (you should run this as Administrator if you are not an admin of the machine).
  • Change to the IDE folder a few levels above the template folder (e.g. to C:\<Program Files>\Microsoft Visual Studio 10.0\Common7\IDE)
  • Run the following command:
    devenv.exe /installvstemplates
Viola! There you have your own custom template for new classes.

6. Automate repetitive/fiddly  tasks

If you are going to do this a lot I would suggest creating a batch file, e.g. in your template folder, to run the update step for you. For example create a text file called UpdateTemplates.bat containing the following 3 lines:

cd "C:\Prograsm Files (x86)\Microsoft Visual Studio 12.0\Common\IDE\"
devenv.exe /installvstemplates
pause

Silverlight MVCVM – Long Live Controllers!

I have been asked to write about a subject I feel very strongly about: Adding Controllers to the MVVM pattern.

To that end I am adding information pages, about our suggested implementation of Controllers in MVVM, which I like to call MVCVM (for “Model, View, Controller, ViewModel”).

The first posting, providing some background information and general concepts, is “Silverlight MVCVM – MVVM With Controllers“.

This has been a debated suggestion on StackOverflow.com, so I welcome your thoughts and suggestions on MVCVM as I provide more and more information about practical uses for it.

Crunch: Online accounting made easy

Crunch, save time, save tax, save money

HiTech Magic Ltd are proud to be an affiliate for Crunch accounting

Stopping a user from leaving a Silverlight page

Like Flash, Silverlight applications only exist for the life of the page that hosts it.

A common complaint is that when you navigate away from your Silverlight page you get no chance to tidy up or warn the user…

Never fear, help is at hand! All browsers expose an event called window.onbeforeunload.

If you connect a method to that event handler, that returns a string, you will get a standard Message box that looks like this (Internet Explorer):

 

 

or like this (Chrome):

Your javascript  onbeforeclose method is expected to provide a string value to display in that Message box. It makes sense to return that from your Silverlight application, so we will actually kill two birds with one stone.

Here are the actual steps you need to hook into the onbeforeunload event…

1. Add a pluginloaded handler to your HTML page hosting the Silverlight application:

	var slCtl = null;
	function pluginLoaded(sender, args) {
		slCtl = sender.getHost();
	}

This basically stores your Silverlight control object in a global (page) variable called slCtrl.

2. Specify the pluginloaded in your Silverlight object definition:

     <param name="onLoad" value="pluginLoaded" />

This ensures the previous handler is actually called.

3. Define an onClose javascript method and assign it to the onbeforeunload event:

	function onClose() {
		return slCtl.Content.App.OnBeforeClose();
		}
	window.onbeforeunload = onClose

4. Register your Silverlight application object with Silverlight in your App.xaml.cs file Application_Startup event:

private void Application_Startup(object sender, StartupEventArgs e)
{
    this.RootVisual = new MainPage();
    HtmlPage.RegisterScriptableObject(“App”, this);
}

5. Expose a scriptable method in your application to do the cleanup and return the string to display:
[ScriptableMember]
public string OnBeforeClose()
{
    // Do some shutdown work here…
    return “This is where your custom message will be displayed”;
}

I am assuming you know how to put the Javascript into appropriate HTML script elements in your file.

This is guaranteed to run before the user gets the “Are you sure…” prompt. Just add your “shutdown” processing before you return from OnShutdown and voilà you now have a place to hook your code!

Unfortunately there is no way to stop the Message box, but it is better than your user leaving your Silverlight application before you want them to.

Microsoft Prism 4.0 in pre-release

Prism is a pattern of modular development, used to produce “very large” applications. The current platforms that Prism targets are WPF and Silverlight. Prism was developed by Microsoft and is available to the development public as an open source project here.

The basic premise of Prism is one of divide and conquer. The divide is cleverly performed by using loose coupling of modules (in English that means that modules know little if anything about each other).

Prism includes a practice referred to as Unity. The Unity container is responsible for providing interfaces to modules and objects on request and acts as the central port of call that modules use to communicate with other modules.

There is a very good set of videos giving examples of Prism on the Channel 9 site here.

The latest version combines a second practice known as MEF (Managed Extensibility Framework). This is another Microsoft produced open source project. MEF uses clever techniques to identify requirements in projects, at runtime, by analysing “import” and “export” directives that exist only in the Metadata of the project. MEF is designed to allow for a plug-in style environment, like the Visual Studio IDE.

We are now using Prism and MEF on a medium to large scale project that is expected to have a lifespan of many years.

We will post updates on techniques as we progress on the project. I must say it’s good to be back on hard-core Silverlight development after a spell of APS.Net development.

Thanks, Dave

WordPress Themes