wpf - 如何使用 Xamarin.Forms、WPF 和 Syncfusion 控件在屏幕上呈现 PDF?
问题描述
我正在编写一个针对 WPF 和 MacOS 的 Xamarin.Forms 应用程序。在共享的 Xamarin.Forms 项目中,我有一个带有属性的基本自定义控件,例如Source
,保存代表我从 Web 服务或文件系统收集的 PDF 的流。在平台特定项目中,我有自定义渲染器,可根据平台特定需求渲染 PDF 文件。
对于 MacOS,这个问题很容易解决。但是一段时间后,我发现 WPF 工具包没有为 PDF 提供本机控件。经过一些研究后,我选择使用Syncfusion PDF 控件来进行 WPF。
我不需要很花哨的东西,只需能够在屏幕上显示 PDF 文件,没有工具栏或侧边栏,因此,按照文档中的说明,我选择使用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");
}
}
}
}
当我将控件放在 a 中ScrollView
并将其强制设置HeightRequest
为 3000 或 4000 时,我可以看到第二页。但无论如何,即使通过强制高度来为其他页面留出空间,它们也不会呈现。我相信这是因为 Syncfusion 控件的虚拟化功能,以提高性能:
但在这种情况下,这似乎可能会影响我应该使用控件的方式。如何正确使用 WPF 的 Syncfusion PDF 查看器,以便在屏幕上呈现包含多个页面的 PDF?现在使用 WebView 不是一个很好的选择,就第三方控件而言,我只能访问 Syncfusion 控件。
解决方案
请参考以下针对您的需求处理相同场景的论坛,并查看信息是否有助于您解决问题。
注意:我为 Syncfusion 工作
推荐阅读
- java - 如何检测akka actor终止是由于系统关闭并避免重新启动它
- python - 如何绘制熊猫 groupby 子集
- reactjs - 无法在事件回调中更新状态
- python - Python ML 向后消除删除错误的数组
- ruby-on-rails - 0:Integer 的未定义方法“each_slice”
- postgresql - 如何将postgres连接到ansible?
- jquery - blockUi 解除阻塞所有被阻塞的元素
- css - 更改光标:指向 CSS 中自定义光标的指针
- python - 在滚动区域小部件的 PyQt5 中添加 n 列
- php - PHP - 跳过类 null 的对象