Fostering development process in .Net

14 07 2014

Fostering development process in .Net

In this article I will explain how to speed up the development process using few fabulous features of Visual Studio IDE.

Code Snippets

What is it?

Have you ever tried writing try, foreach, while or any other keyword and than hitting tab after it. Do it and you will define what Code Snippet is and will realize what it can do.

Code Snippet is nothing but a predefined set of code that is plugged into the surface area by using a shortcut key defined for it.

When to use?

A set of code with a constant pattern but variable custom literals can be translated to code snippets. Example

for (int i = 0; i < length; i++)


The structure of for statement remains same but the variables used inside it needs to be changed. In the above code i and length would be replaced by the some other variable inline with your code. So whenever you have such kind of code that needs to be plugged in really quickly you can create a snippet for that.

How to use?

Snippets are basically and XML based files with .snippet extension. It contains two major sections Header and Snippet.

Header : This section includes the details regarding the snippet that is being used. Some details like Title of the snippet, Shortcut key for the snippet, author name and snippet type are defined in this section.

Snippet : This is the section where in the actual magic of creating the snippet is defined.

It includes other four subsections viz

  • Code Defines the language type for the snippet and the code to be plugged in.  is used for the actual code to be inserted when the snippet is used. Literals to be replaced are prefixed and suffixed by $ sign.
  • Declarations Defines the literals that need to be replaced. This appears green in color in the surface are. One can define the tooltip, default value etc pertaining to the literals in this section.
  • Import Defines the namespaces that will be automatically included if your snippet is used.
  • References Defines the dll to be included for the namespace added using Import statement

Below steps will drive you through the process of creating a sample snippet.

Step 1

Create file with .snippet extensions with below xml

Step 2

Open Visual Studio and click Ctrl+k b. This will open Code Snippet Manager window.

Step 3

Click Import and browse through the file created in step 1 and Click Ok

Step 4

Now Choose the location where in you want to categories your snippet and click ok.

If you choose Visual C# than the file would be copied to /%Visual Studio%/ VC#\Snippets\1033\Visual C# folder

Step 5

Go to the code are where in you want to use your code snippet and type replace and than hit tab key.


Code Block

What is it?

If you do not want to go by the approach of code snippets, than you can quickly achieve the same functionality by Code Blocks. Code Blocks are nothing but a predefined code that needs to be used with some refactoring.

How to use?

Creating the code blocks is simply a drag drop process.

Step 1

Select the code that you want to reuse.

Step 2

Drop it onto the toolbar. Visual Studio will create the toolbar item for the block you have copied. Rename the block to meaningful name.

Step 3

Now wherever you want to plug in the code just click on the block.


Enjoy and Speed up your development.


Printing in Silverlight

14 07 2014

Printing in Silverlight

Printing is a very basic requirement for business application now days. At the advent of Silverlight there was no direct support for printing. But in Silverlight version 4 Microsoft have provided Print Document assembly for achieving the printing functionality.

What if the application still uses Silverlight version 3.0 or earlier versions?

In this article I will discuss the alternate approach for printing in Silverlight version 3.0 and earlier and then move on to a more sophisticated approach using Print Document provided in Silverlight 4.0.

The alternative is bit simple but not that much mature. Here we will take the entire image of the control that needs to be printed and let the user to save it using the save dialog. But one major problem with this approach is related to scaling of the image. In a case that have a large number of records to be printed like 100 records in data grid than the height of the jpeg produce would be very high and when it would be printed then it would automatically scale down the size of the image to fit in one single print page. The clarity for the scaled down image would not be good enough for reading purpose. So to overcome this thing I developed an algorithm to split the big heighted image into fixed slices and then I have used PdfSharp library to send the each sliced images to separate pages in pdf file. Finally the user will be able to save the Pdf file from where he can print the document. But here the chances to cut down the data when the image is splitted would remain.

Due to such pain and alternate unorganized approaches Microsoft finally Gifted Silverlight 4.0 with Print Document class. This class provides printing capabilities in Silverlight Applications. Refer for more details.

I strived hard to develop one such common framework related to print document class till I found one fantastic article by Pete Brown in his blog Creating a Simple Report Writer in Silverlight 4

This framework allows printing for one item source with one data template. My requirement forced me to print multiple grids at the same time in one single print job. So I analyzed the framework and with some modifications finally achieved it. I have converted some of its properties to arrays which use combination of multiple Data Templates and Item Sources to print multiple grids in one single print job.

Thanks Peter for such a fabulous use of Print Document.