0

我有一个 WPF 数据输入屏幕/窗口,我想为其提供打印输出。(准确地说,它是一个用户控件,我已经将它作为我要打印的 TabControl 中的 TabItem 来工作。)

为了打印得很好,我做了几个布局转换,将窗口缩放到纸张大小,并更改应用程序的皮肤。“打印皮肤”将背景更改为白色,并删除标题标签上的背景等。

这非常有效 - 虽然我有MessageBox.Show()提示告诉我发生了什么。

然而,当我拿出Messagebox.Show提示时,我发现我所有的打印魔法都不起作用了,就好像整个打印方法只是:PrintVisual();在UserControl上。(我已将其范围缩小到一个 MessageBox,如果不破坏事物就无法摆脱它。)

代码(抱歉这么长,我添加了批量评论):

private void PrintStatements()
{
  PrintDialog print = new PrintDialog();

  /// Needed data
  PrintCapabilities capabilities = print.PrintQueue.GetPrintCapabilities(print.PrintTicket);
  double pageMargin = 1 / 2.54;   // 1cm
  double pageWidth = capabilities.PageImageableArea.ExtentWidth - (pageMargin);
  double pageLength = capabilities.PageImageableArea.ExtentHeight - (pageMargin);
  Size pageSize = new Size(pageWidth, pageLength);
  ResourceDictionary resources = new ResourceDictionary();

  bool canPrint = print.ShowDialog() ?? false;
  if (canPrint)
  {
    double tabWidth = StatementsTabCtrl.ActualWidth;
    double tabHeight = StatementsTabCtrl.ActualHeight;

    /// 1.  Get tab item content ('currentTabContent') 
    StatementsTabItem currentTabContent = StatementsTabCtrl.SelectedContent as StatementsTabItem;

    /// 2.  Get Original Specs of currentTabContent
    ResourceDictionary origSkin = Application.Current.Resources;
    Size origSize = new Size(currentTabContent.ActualWidth, currentTabContent.ActualHeight);
    Transform origTransform = currentTabContent.LayoutTransform;

    /// 3.  Transform currentTabContent (expose print panels, move stuff around etc) - 
        //do this inside TabItem class
    currentTabContent.SetupPrinting();

    /// 4.  Make changes outside TabItem (skin, page-scale)
        // skin
    resources.MergedDictionaries.Add(Application.LoadComponent(new Uri(@"Skin/PrintDictionary.xaml", UriKind.Relative)) as ResourceDictionary);
    Application.Current.Resources = resources;
        // page-scale
    double scale = Math.Min(pageWidth / currentTabContent.ActualWidth,
                            pageLength / currentTabContent.ActualHeight);

    // ****Uncomment next line - printing magic works.**** //
    // MessageBox.Show(string.Format("scaling by {0}", scale));
    System.Threading.Thread.Sleep(250);

    currentTabContent.LayoutTransform = new ScaleTransform(scale, scale);

    //Measure and Arrange
    currentTabContent.Measure(pageSize);
    ((UIElement)currentTabContent).Arrange(new Rect(
         new Point((capabilities.PageImageableArea.OriginWidth + pageMargin),
             (capabilities.PageImageableArea.OriginHeight + pageMargin)), pageSize));

    System.Threading.Thread.Sleep(250);

    /// 5. Print (Finally!)
    print.PrintVisual(currentTabContent, "Print Results");

    /// 6. Return everything to normal (undo 3, then 4)
        // undo 3.
    currentTabContent.TearDownPrinting();
        // undo 4.
    Application.Current.Resources = origSkin;
    scale = Math.Max(currentTabContent.ActualWidth / pageWidth,
                                    currentTabContent.ActualHeight / pageLength);
    currentTabContent.LayoutTransform = new ScaleTransform(1 / scale, 1 / scale);

    currentTabContent.Measure(origSize);
    ((UIElement)currentTabContent).Arrange(new Rect(0, 0, tabWidth, tabHeight));
  }
}

如您所见,我尝试添加一些System.Threading.Thread.Sleep(250);以防需要更多时间来识别更改,但这没有任何效果。电话的怪异PrintDialog()是出于类似的原因。以通常的方式调用它没有区别。

谁能告诉我为什么我的打印功能在没有 MessageBox 调用的情况下无法工作,或者告诉我如何模拟 MessageBox 调用以使程序正常工作?很多很多 TIA

4

1 回答 1

0

您可能缺少的部分是MessageBox.Show();刷新了 UIElement

因此,如果您使用以下代码手动刷新 UIElement 就足够了

  ui.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));// activates ui.InvalidateMeasure(); 
  ui.Arrange(new Rect(new Point(0, 0), ui.DesiredSize));                 // activates ui.InvalidateArrange();

  ui.UpdateLayout(); // <-- refreshed your UIElement

这至少在我的情况下有效

旁注:
您不应该直接调用,ui.Invalidate...因为如果您不更改 Measure 或 Arrange 因为UpdateLayout()不会刷新您的UIElementAFAIK

于 2014-02-18T14:42:27.747 回答