我使用下面的代码很多年了,但是突然停止显示图像。我做了一个简短的版本来测试它并解释:
有一个UserControl
XamlPage
,它只包含两个控件:
<StackPanel>
<TextBlock Text="My Content"/>
<Image Source="/Images/flower.jpg" Height="100"/>
</StackPanel>
XamlPage
用于DocumentPaginator
类的派生类中:
public class Paginator : DocumentPaginator
{
double pageHeight = 1122.52;
double pageWidth = 793.7;
Size mySize;
public Paginator()
{
mySize = new Size(pageWidth, pageHeight);
}
public override DocumentPage GetPage(int pageNumber)
{
XamlPage page = new XamlPage();
page.Measure(PageSize);
page.Arrange(new Rect(new Point(0, 0), PageSize));
page.UpdateLayout();
return new DocumentPage(page, PageSize, new Rect(0, 0, 10, 10), new Rect());
}
public override bool IsPageCountValid
{
get { return true; }
}
public override int PageCount
{
get { return 1; }
}
public override Size PageSize
{
get { return mySize; }
set { mySize = value; }
}
public override IDocumentPaginatorSource Source
{
get { return null; }
}
}
在主窗口中,只有两个按钮,一个打开使用XamlPage
控件的新窗口,另一个打开Preview
窗口。按钮绑定到MainVM
中的Commands
:
private void PreviewCommandAction()
{
Paginator paginator = new Paginator();
MemoryStream lMemoryStream = new MemoryStream();
Package package = Package.Open(lMemoryStream, FileMode.Create);
XpsDocument xpsDocument = new XpsDocument(package);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
writer.Write(paginator);
xpsDocument.Close();
package.Close();
Preview previewWindow = new Preview(lMemoryStream);
previewWindow.ShowDialog();
}
private void WindowCommandCommandAction()
{
XamlWindow xw = new XamlWindow();
xw.Show();
}
Preview
窗口仅包含一个控件:
<DocumentViewer Document="{Binding Document}"/>
而PreviewVM
:
public class PreviewVM : ObservableObject
{
private IDocumentPaginatorSource myDocument;
private XpsDocument xps;
private Uri packageUri;
private Stream docStream;
public PreviewVM(MemoryStream xpsStreamDocument)
{
docStream = xpsStreamDocument;
Package package = Package.Open(docStream);
//Create URI for Xps Package
//Any Uri will actually be fine here. It acts as a place holder for the
//Uri of the package inside of the PackageStore
string inMemoryPackageName = string.Format("memorystream://{0}.xps", Guid.NewGuid());
packageUri = new Uri(inMemoryPackageName);
//Add package to PackageStore
PackageStore.AddPackage(packageUri, package);
xps = new XpsDocument(package, CompressionOption.SuperFast, inMemoryPackageName);
Document = xps.GetFixedDocumentSequence();
}
public IDocumentPaginatorSource Document
{
get { return myDocument; }
set
{
if (myDocument == value) return;
myDocument = value;
OnPropertyChanged(nameof(Document));
}
}
}
因此,在MainVM
中,WindowCommandCommandAction
方法打开一个新窗口并显示UserControl
XamlPage
,没有任何意外。但PreviewCommandAction
方法正确打开Preview
窗口,此预览仅显示TextBox
的内容,而不显示Image
。没有错误消息,Windows事件查看器中没有任何内容。我再说一遍,直到两天前,这一直在工作。
顺便问一下,有没有其他的解决方案来获得预览?我需要使用DocumentPaginator来管理页面。
整个测试项目位于此存储库中:
https://github.com/yannicklal/TestPreview
1条答案
按热度按时间e0bqpujr1#
由于2022年12月13日的更新,这是微软的问题。
KB5022083 Change in how WPF-based applications render XPS documents