Silverlight 4 Printing API

•November 22, 2009 • Leave a Comment

One of the new features of Silverlight 4 is the printing API. This is really a huge thing because printing in previous version was really pain to do. So I decided to play around with the API to see what the possibilities are.

In my example I created a “Hello World” application that contains a textblock and a button. By pressing the button the system will print the screen.

XAML:

<Grid x:Name=”LayoutRoot” Background=”White”>

<Grid.RowDefinitions>

<RowDefinition Height=”auto” />

<RowDefinition Height=”auto” />

</Grid.RowDefinitions>

<StackPanel Margin=”10 ” Orientation=”Horizontal” Grid.Row=”0″ >

<TextBlock Name=”txtTextToPrint” Margin=”5″ Text=”Hello World! I am printing from Silverlight.”></TextBlock>

<Button Click=”Button_Click” Content=”Print”></Button>

</StackPanel>

</Grid>

Code behind:

private void Button_Click(object sender, RoutedEventArgs e)

{

PrintDocument pd = new PrintDocument();

pd.DocumentName = “SilverlightPrintTest”;

pd.PrintPage += new EventHandler<PrintPageEventArgs>(pd_PrintPage);

pd.Print();

}

void pd_PrintPage(object sender, PrintPageEventArgs e)

{

e.PageVisual = LayoutRoot;

}

Print output :

This code lets me print the whole screen that you see because I set the PageVisual = LayoutRoot. The PageVisual property excepts a UIElement so if you only want to print a specific control then you do e.PageVisual = nameOfYourControl. In fact in my sample the LayoutRoot is the root UEIlement of the page.

In my sample  I use the new PrintDocument class. This class is located in the System.Windows.Printing namespace.  The most important things on this class are the following 3 events:

  • PrintPage: occurs each time a page is printing.
  • StartPrint: occurs before the PrintPage event.
  • EndPrint: occurs after the PrintPage event. Is mostly used to handle any errors that occurred during printing.

Mike Taulty has a more in depth sample in this blog post: http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2009/11/18/silverlight-4-rough-notes-printing.aspx

ASP.NET MVC 2 preview 1 released

•August 6, 2009 • 1 Comment

Microsoft has released ASP.NET MVC 2 preview 1. I am quite late with the announcement but I have been busy. There are some nice features in this release like:

  • Templated Helpers
  • Areas
  • Support for DataAnnotations Attributes

I didn’t had the time yet to explore the new release but you can find more details on Phil Haack blog. Here is the link. I will post more about this subject in the future.

ASP.NET MVC Tip of the day

•July 30, 2009 • 1 Comment

By default ASP.NET MVC doesn’t compile views. So if there are compile errors in your .aspx or .ascx you will see them at runtime which can be very painful :-) . Luckily there is an option in your Visual Studio 2008 project file that lets you compile views. This option is called MvcBuildViews.

Visual Studio project file

Visual Studio project file

The above screenshot shows the content of a Visual Studio 2008 project file. You just have to open it with a text editor (example: Notepad). Now if you set the value of property MvcBuildView to true the compiler will compile your views.

“Project type is not supported by this installation” problem

•July 17, 2009 • 4 Comments

Recently I had a problem with my Visual Studio 2008. The problem was that I couldn’t open any ASP.NET Web Application projects. The error message I received when trying was the following: “Project type is not supported by this installation”. After some googling I found the following command: “devenv /setup”. So what does this command do? Actually it resets your VSPackages and also you project templates. For me it fixed the problem because my project template returned. You need to type this command in the Visual Studio 2008 Command Prompt.

Hello World!

•December 17, 2008 • Leave a Comment

This is my first wordpress post! I hope many more will come :-)