简体   繁体   中英

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:

在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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM