Yan*_*ick 4 wpf preview mvvm documentpaginator
我使用下面的代码多年,但突然停止显示图像。我做了一个简短的版本来测试它并解释:
有一个UserControl XamlPage只包含两个控件:
<StackPanel>
<TextBlock Text="My Content"/>
<Image Source="/Images/flower.jpg" Height="100"/>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
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; }
}
}
Run Code Online (Sandbox Code Playgroud)
在主窗口中,只有两个按钮,一个使用XamlPage控件打开新窗口,另一个打开Preview窗口。这些按钮绑定Commands到MainVM:
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();
}
Run Code Online (Sandbox Code Playgroud)
该Preview窗口仅包含一个控件:
<DocumentViewer Document="{Binding Document}"/>
Run Code Online (Sandbox Code Playgroud)
还有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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此,在该MainVM方法中WindowCommandCommandAction打开一个新窗口并显示它,UserControl XamlPage没有任何惊喜。但该PreviewCommandAction方法正确打开Preview窗口,并且此预览仅显示内容,TextBox但不显示Image. 没有错误消息,Windows 事件查看器中没有任何内容。我再说一遍,直到两天前,这种方法已经有效了很多年。
顺便问一下,还有其他解决方案来获取预览吗?我需要使用 DocumentPaginator 来管理页面。
整个测试项目都在这个存储库中:
这是由于 2022 年 12 月 13 日更新而导致的 Microsoft 问题。
KB5022083 基于 WPF 的应用程序呈现 XPS 文档的方式发生变化
然后做一个包含以下内容的.reg文件解决了问题并且可以轻松喷洒:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\Windows Presentation Foundation\XPSAllowedTypes]
"DisableDec2022Patch"="*"
Run Code Online (Sandbox Code Playgroud)
编辑
该问题在这些更新中得到解决:
对于 Windows 10: 2023 年 1 月 31 日-KB5023366 更新
对于 Windows 11: 2023 年 1 月 31 日-KB5023320 更新
| 归档时间: |
|
| 查看次数: |
873 次 |
| 最近记录: |