北京网站建设一条龙,网站后台生成器,wordpress手机端怎么用,常用的平面设计软件有哪些背景 某些情况下#xff0c;需要让用户阅读下发的文件#xff0c;特别是红头文件#xff0c;这些文件一般都是使用PDF格式下发#xff0c;这种文件有很重要的一点就是不能更改。这时候就需要使用原文件进行展示。 Xamarin.Forms Android 中的 WebView 控件是不能直接显示的…背景 某些情况下需要让用户阅读下发的文件特别是红头文件这些文件一般都是使用PDF格式下发这种文件有很重要的一点就是不能更改。这时候就需要使用原文件进行展示。 Xamarin.Forms Android 中的 WebView 控件是不能直接显示的但是这并不影响因为最终文件的显示依托于它的继承类 View 类。 解决方案
需要的 Nuget 包
XamarinLibrary.Xamarin.Android.Github.Barteksc.AndroidPdfViewer Xamarin.Android.Support.v4
AndroidPdfViewer 包目前还是预览版需要去使用命令安装 Support.v4 包不是必须当系统目标版本大于 Android9.0 时才需要引入
在 UI 项目里要做的操作 创建一个自定义控件继承 View没了。对就这些 然后在视图中使用这自定义控件。 在平台 Android 项目里要做的操作
在 Resources 创建 layout 文件夹在 layout 文件里添加 PDFLayout.xml 文件创建 Renderers 文件夹并添加 XFAPDFViewRenderer.cs 文件PDF文件放在 Assets 文件夹下
文件说明
PDFLayout.xml 属性设置
生成操作AndroidResource 自定义工具MSBuild:UpdateGeneratedFiles PDF文件设置 设置生成操作AndroidAsset 即可 PDFLayout.xml 文件内容
?xml version1.0 encodingutf-8?
RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentcom.github.barteksc.pdfviewer.PDFViewandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:idid/pdfView/
/RelativeLayout XFAPDFViewRenderer.cs文件内容
using Android.Content;
using Com.Github.Barteksc.Pdfviewer;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using XFA.PdfTest.Controls;
using XFA.PdfTest.Droid.Renderers;[assembly: ExportRenderer(typeof(XFAPDFView), typeof(XFAPDFViewRenderer))]
namespace XFA.PdfTest.Droid.Renderers
{public class XFAPDFViewRenderer : ViewRendererXFAPDFView, Android.Widget.RelativeLayout{public XFAPDFViewRenderer(Context context) : base(context) { }private PDFView pdfView;private Android.Widget.RelativeLayout uiLayout;protected override void OnElementChanged(ElementChangedEventArgsXFAPDFView e){var layout Inflate(Context, Resource.Layout.PDFLayout, null);if (pdfView null){uiLayout layout as Android.Widget.RelativeLayout;}pdfView uiLayout.FindViewByIdPDFView(Resource.Id.pdfView);pdfView.FromAsset(XXX.pdf).Load();SetNativeControl(uiLayout);}}
}结语
在 Xamarin.Forsm for Android 中显示 PDF 并不像 for IOS 中可以直接使用 WebView 进行显示所以使用 WebView 是行不通的。多搜索多探索看看别人的案例虽然不能百分之百解决问题但可以提供很有效的思路。