Tuesday, January 29, 2013

Printing in Silverlight/Lightswitch using XAML and custom controls

Printing in Silverlight/Lightswitch using XAML and custom controls

I am programming a Silverlight application using Lightswitch. I want to print the data which is displayed on some screens.

I found this tutorial for printing in Silverlight/Lightswitch. It describes how to create a custom control using XAML which can be printed. The control looks like this:

Printing in Lightswitch

In the background you can see the control how it looks like in the Silverlight application. The control contrains both a button and a grid:

<StackPanel>     <Button Content="Print" Name="btnPrint" Click="btnPrint_Click" />         <Grid x:Name="LayoutRoot">             <!-- grid code goes here -->             <!-- some more code an closing tags --> 

Using Silverlight's printing API, printing is done like this in the custom control:

PrintDocument printInvoice = new PrintDocument(); private void btnPrint_Click(object sender, System.Windows.RoutedEventArgs e){        printInvoice.PrintPage +=         new EventHandler<PrintPageEventArgs>(printInvoice_PrintPage); }  void printInvoice_PrintPage(object sender, PrintPageEventArgs e){     e.PageVisual = LayoutRoot; } 

Since e.PageVisual = LayoutRoot is used, we only see the table in the printed output, and not the button. That's okay, but I would like to use a seperate XAML for the print layout. My goal is to just show a button Print on the Silverlight application, and define the print layout in a seperate XAML.

So, I just started to create a second XAML as SilverlightControl and tried to use it:

MyPrintLayout mpl = new MyPrintLayout(); void printArtikels_PrintPage(object sender, PrintPageEventArgs e){     e.PageVisual = mpl.LayoutRoot; } 

But I get the error "Das Element ist bereits das untergeordnete Element eines anderen Elements" (english: "The element is already the sub-element of another element"). This error was discussed in this question as well, but it does not solve my problem.

When I include MyPrintLayout in the silverlight application it is displayed without a problem (there is only some text in it to test the functionality).

It seems like I am doing this completely wrong. How can I achieve my goal?

Answers & Comments...

Answer: 1

mpl.LayoutRoot already sub-element of mpl. Try this:

void printArtikels_PrintPage(object sender, PrintPageEventArgs e){     MyPrintLayout mpl = new MyPrintLayout();     e.PageVisual = mpl; } 
by : Memoizerhttp://stackoverflow.com/users/1937750




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog