如何在Silverlight的单独页面中打印集合中的每个对象

本文关键字:打印 集合 对象 Silverlight 单独页 | 更新日期: 2023-09-27 18:19:04

我想知道是否有可能使用Silverlight打印API在单独的页面中打印集合中的每个对象。

假设我有一个类Label

public class Label
{
    public string Address { get; set; }    
    public string Country { get; set; }    
    public string Name { get; set; }    
    public string Town { get; set; }
}

我可以使用打印API并像这样打印。

private PrintDocument pd;
private void PrintButton_Click(object sender, RoutedEventArgs e)
{
   pd.Print("Test Print");
}
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
    Label labelToPrint = new Label() 
    { 
        Name = "Fake Name", Address = "Fake Address",
        Country = "Fake Country", Town = "Town"
    };
    var printpage = new LabelPrint();
    printpage.DataContext = new LabelPrintViewModel(labelToPrint);
    e.PageVisual = printpage;
}

LabelPrint Xaml

<StackPanel x:Name="LayoutRoot" VerticalAlignment="Center"
            HorizontalAlignment="Center">
    <TextBlock Text="{Binding Name}" />
    <TextBlock Text="{Binding Address}" />
    <TextBlock Text="{Binding Town}" />
    <TextBlock Text="{Binding Country}" />
</StackPanel>

现在,假设我有一个Label对象集合,

List<Label> labels = new List<Label>() 
{
    labelToPrint, labelToPrint, labelToPrint, labelToPrint
};

如何在单独的页面上打印列表中的每个对象?

谢谢你的建议。

如何在Silverlight的单独页面中打印集合中的每个对象

你可以有一个多页打印机。

private List<Label> printLabels;    
private PrintDocument pd;
private void PrintButton_Click(object sender, RoutedEventArgs e)
{
   // save the labels to a temporary list
   printLabels = new List<Label>(labels);
   // start the printing
   pd.Print("Test Print");
}
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
    // print the first element from the temporary list
    Label labelToPrint = printLabels.First();
    var printpage = new LabelPrint();
    printpage.DataContext = new LabelPrintViewModel(labelToPrint);
    e.PageVisual = printpage;
    printLabels.Remove(labelToPrint);
    // continue printing if there's still any labels left
    e.HasMorePages = printLabels.Any();
}