【发布时间】:2020-11-27 02:16:22
【问题描述】:
我正在编写一个针对 WPF 和 MacOS 的 Xamarin.Forms 应用程序。在共享的 Xamarin.Forms 项目中,我有一个基本的自定义控件,其中包含 Source 等属性,它保存代表我从 Web 服务或文件系统收集的 PDF 的流。在平台特定项目中,我有自定义渲染器,可根据平台特定需求渲染 PDF 文件。
对于 MacOS,这个问题很容易解决。但是一段时间后,我发现 WPF 工具包没有为 PDF 提供本机控件。经过一番研究,我选择使用Syncfusion PDF control for WPF。
我不需要很花哨的东西,只要能够在屏幕上显示一个 PDF 文件,没有工具栏或侧边栏,所以,following the instructions in the documentation 我选择使用PdfDocumentView 来加载 PDF 文件。
但是,当使用PdfViwerControl 或PdfDocumentView 时,我似乎一次无法加载超过两个页面。这些控件的垂直滚动条从不显示,但水平滚动条工作正常。
这是我到目前为止的代码(加载本地硬编码的本地文件以进行测试):
查看
<?xml version="1.0" encoding="UTF-8" ?>
<Grid
x:Class="MyApp.Views.DocumentOverview"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="MyApp.Controls">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
</Grid>
<Grid Grid.Row="1" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<controls:CustomPdfViewer x:Name="myPdfViewer"
BackgroundColor="Transparent"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand" />
</Grid>
</Grid>
控制
namespace MyApp.Controls
{
public class CustomPdfViewer : ContentView
{
//Still to implement the Stream property
}
}
自定义渲染器
[assembly: ExportRenderer(typeof(CustomPdfViewer), typeof(PdfLoaderRenderer))]
namespace MyApp.WPF.CustomRenderers
{
class PdfLoaderRenderer : ViewRenderer<CustomPdfViewer, PdfDocumentView>
{
PdfDocumentView _pdfDocument = new PdfDocumentView();
protected override void OnElementChanged(ElementChangedEventArgs<CustomPdfViewer> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
SetNativeControl(_pdfDocument);
}
}
protected async override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName.Equals(nameof(Element.Source)))
{
await LoadFile();
}
base.OnElementPropertyChanged(sender, e);
}
private async Task LoadFile()
{
if (Element != null && Element.Source != null)
{
_pdfDocument.Load(@"C:\Sources\Document.pdf");
}
}
}
}
当我将控件放在 ScrollView 中并将 HeightRequest 强制设置为 3000 或 4000 时,我可以看到第二页。但无论如何,即使通过强制高度有其他页面的空间,它们没有被渲染。我相信这是因为 Syncfusion 控件的虚拟化功能,以提高性能:
但似乎在这种情况下可能会影响我应该使用控件的方式。如何正确使用 WPF 的 Syncfusion PDF 查看器,以便在屏幕上呈现具有多个页面的 PDF?现在使用 WebView 不是一个很好的选择,就第三方控件而言,我只能访问 Syncfusion 控件。
【问题讨论】:
标签: wpf pdf xamarin xamarin.forms syncfusion