打印使用 OnLoaded 进行渲染的 UserControl
我正在尝试打印我在代码中创建的 WPF UserControl。我在帖子末尾使用 DocumentPaginator 实现来执行此操作。
我遇到的问题是 DocumentPage 类没有在我的 UserControl 上调用 Loaded,因此该控件没有完全呈现。我无法将 OnLoaded 中的代码移动到更好的位置,因为我不是该控件的维护者。
在控件上触发 Loaded 事件的正确方法是什么?我意识到只有当控件附加到PresentationSource 时才会触发该事件,但我试图发送打印作业而不在屏幕上显示内容。我可能可以在某处使用 Visibility=Hidden 来渲染控件,但这感觉很便宜而且很麻烦。
class MyPaginator : DocumentPaginator
{
public MyPaginator()
{
PageSize = new Size(800, 600);
}
public override DocumentPage GetPage(int pageNumber)
{
var uc = new ChartUC();
uc.Measure(PageSize);
uc.Arrange(new Rect(PageSize));
uc.UpdateLayout();
return new DocumentPage(uc, PageSize, new Rect(PageSize), new Rect(PageSize));
}
public override bool IsPageCountValid
{
get { return true; }
}
public override int PageCount
{
get { return 1; }
}
public override System.Windows.Size PageSize
{
get;
set;
}
public override IDocumentPaginatorSource Source
{
get { return null; }
}
}
I'm trying to print a WPF UserControl that I am creating in code. I am doing this with the DocumentPaginator implementation at the end of the post.
The problem I have is that the DocumentPage class is not calling Loaded on my UserControl so the control isn't completely rendering. I can't move the code in OnLoaded to a better location because I am not the maintainer of the control.
What is the right way to fire a Loaded event on the control? I realise the event is only fired when the control is attached to a PresentationSource, but I am trying to send off the print job with out showing the content on screen. I could probably render the control with Visibility=Hidden somewhere, but that feels cheap and hack-y.
class MyPaginator : DocumentPaginator
{
public MyPaginator()
{
PageSize = new Size(800, 600);
}
public override DocumentPage GetPage(int pageNumber)
{
var uc = new ChartUC();
uc.Measure(PageSize);
uc.Arrange(new Rect(PageSize));
uc.UpdateLayout();
return new DocumentPage(uc, PageSize, new Rect(PageSize), new Rect(PageSize));
}
public override bool IsPageCountValid
{
get { return true; }
}
public override int PageCount
{
get { return 1; }
}
public override System.Windows.Size PageSize
{
get;
set;
}
public override IDocumentPaginatorSource Source
{
get { return null; }
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论