1
0
Fork 0
mirror of https://github.com/Nioux/AideDeJeu.git synced 2025-10-30 23:16:09 +00:00
AideDeJeu/AideDeJeu/AideDeJeu.iOS/PdfViewRenderer.cs
2019-05-13 13:52:58 +02:00

72 lines
No EOL
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using AideDeJeu.iOS;
using AideDeJeu.Views;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(PdfView), typeof(PdfViewRenderer))]
namespace AideDeJeu.iOS
{
public class PdfViewRenderer : ViewRenderer<PdfView, UIWebView>
{
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if(e.PropertyName == "Uri")
{
var pdfView = Element as PdfView;
if (pdfView?.Uri != null)
{
LoadFile(pdfView.Uri);
}
}
}
protected override void OnElementChanged(ElementChangedEventArgs<PdfView> e)
{
base.OnElementChanged(e);
if (Control == null)
{
SetNativeControl(new UIWebView());
}
if (e.OldElement != null)
{
// Cleanup
}
if (e.NewElement != null)
{
var pdfView = Element as PdfView;
if (pdfView?.Uri != null)
{
LoadFile(pdfView.Uri);
}
}
}
void LoadFile(string fileName)
{
string filePath = Path.Combine(Xamarin.Essentials.FileSystem.CacheDirectory, string.Format("pdf/{0}", WebUtility.UrlEncode(fileName)));
Control.LoadRequest(new NSUrlRequest(new NSUrl(filePath, false)));
Control.ScalesPageToFit = true;
}
void LoadPdfJS(string fileName)
{
string filePath = NSBundle.MainBundle.ResourceUrl.Append("pdfjs/web/viewer.html", false).Path;
//string filePath = Path.Combine(Xamarin.Essentials.FileSystem.CacheDirectory, string.Format("pdf/{0}", WebUtility.UrlEncode(fileName)));
Control.LoadRequest(new NSUrlRequest(new NSUrl(filePath, false)));
Control.ScalesPageToFit = true;
}
}
}