void SegmentValueChanged(object sender, EventArgs e)
        {
            if (_viewSegment.SelectedSegment == 0)
            {
                if (_previewView != null)
                {
                    _previewView.RemoveFromSuperview();
                    _previewView.Dispose();
                    _previewView = null;
                }

                Add(TextView);
                TextView.BecomeFirstResponder();
            }
            else
            {
                if (_previewView == null)
                {
                    _previewView = new WKWebView(this.View.Bounds, new WKWebViewConfiguration());
                }

                TextView.RemoveFromSuperview();
                Add(_previewView);

                var markdownService = Mvx.Resolve <IMarkdownService>();
                var markdownText = markdownService.Convert(Text);
                var model = new DescriptionModel(markdownText, (int)UIFont.PreferredSubheadline.PointSize);
                var view = new MarkdownView {
                    Model = model
                }.GenerateString();
                _previewView.LoadHtmlString(view, NSBundle.MainBundle.BundleUrl);
            }
        }
        void SegmentValueChanged()
        {
            if (_viewSegment.SelectedSegment == 0)
            {
                if (_previewView != null)
                {
                    _previewView.RemoveFromSuperview();
                    _previewView.Dispose();
                    _previewView = null;
                }

                Add(TextView);
                TextView.BecomeFirstResponder();
            }
            else
            {
                if (_previewView == null)
                {
                    _previewView = new WKWebView(this.View.Bounds, new WKWebViewConfiguration());
                }

                TextView.RemoveFromSuperview();
                Add(_previewView);

                LoadPreview(_previewView).ToBackground();
            }
        }
Пример #3
0
 public override void ViewDidUnload()
 {
     if (webView != null)
     {
         webView.Dispose();
     }
     base.ViewDidUnload();
 }
Пример #4
0
        async Task NavigationComplete(WKWebView webView, string filename, PageSize pageSize, PageMargin margin, TaskCompletionSource <ToFileResult> taskCompletionSource)
        {
            try
            {
                var widthString = await webView.EvaluateJavaScriptAsync("document.documentElement.offsetWidth");

                var width = double.Parse(widthString.ToString());

                var heightString = await webView.EvaluateJavaScriptAsync("document.documentElement.offsetHeight");

                var height = double.Parse(heightString.ToString());

                if (width < 1 || height < 1)
                {
                    taskCompletionSource.SetResult(new ToFileResult(true, "WebView has zero width or height"));
                    return;
                }

                webView.ClipsToBounds            = false;
                webView.ScrollView.ClipsToBounds = false;

                var bounds = webView.Bounds;
                webView.Bounds = new CGRect(0, 0, (nfloat)width, (nfloat)height);

                var scale = pageSize.Width / width;


                var snapshotConfig = new WKSnapshotConfiguration
                {
                    SnapshotWidth = pageSize.Width / Display.Scale
                };

                var image = await webView.TakeSnapshotAsync(snapshotConfig);

                if (image.AsPNG() is NSData data)
                {
                    var path = Path.Combine(ToPngService.FolderPath(), filename + ".png");
                    File.WriteAllBytes(path, data.ToArray());
                    taskCompletionSource.SetResult(new ToFileResult(false, path));
                    return;
                }
                webView.Bounds = bounds;
                taskCompletionSource.SetResult(new ToFileResult(true, "No data returned."));
            }
            catch (Exception e)
            {
                taskCompletionSource.SetResult(new ToFileResult(true, "Exception: " + e.Message + (e.InnerException != null
                    ? "Inner exception: " + e.InnerException.Message
                    : null)));
            }
            finally
            {
                webView.Dispose();
            }
        }
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            if (disposing)
            {
                if (_wkWebView != null)
                {
                    _wkWebView.Dispose();
                    _wkWebView = null;
                }
            }
        }
Пример #6
0
        /*
         * public void ToPdf(TaskCompletionSource<ToFileResult> taskCompletionSource, WebView xfWebView, string fileName)
         * {
         *  if (xfWebView.Effects.Any(e=>e is Forms9Patch.WebViewPrintEffect))
         *  {
         *
         *  }
         * }
         */

        async Task NavigationComplete(WKWebView webView, string filename, PageSize pageSize, PageMargin margin, TaskCompletionSource <ToFileResult> taskCompletionSource)
        {
            try
            {
                var widthString = await webView.EvaluateJavaScriptAsync("document.documentElement.offsetWidth");

                var width = double.Parse(widthString.ToString());

                var heightString = await webView.EvaluateJavaScriptAsync("document.documentElement.offsetHeight");

                var height = double.Parse(heightString.ToString());

                if (width < 1 || height < 1)
                {
                    taskCompletionSource.SetResult(new ToFileResult(true, "WebView has zero width or height"));
                    return;
                }

                webView.ClipsToBounds            = false;
                webView.ScrollView.ClipsToBounds = false;

                if (webView.CreatePdfFile(webView.ViewPrintFormatter, pageSize, margin) is NSMutableData data)
                {
                    var path = System.IO.Path.Combine(ToPngService.FolderPath(), filename + ".pdf");
                    System.IO.File.WriteAllBytes(path, data.ToArray());
                    taskCompletionSource.SetResult(new ToFileResult(false, path));
                    data.Dispose();
                    return;
                }
                taskCompletionSource.SetResult(new ToFileResult(true, "No data returned."));
            }
            catch (Exception e)
            {
                taskCompletionSource.SetResult(new ToFileResult(true, "Exception: " + e.Message + (e.InnerException != null
                    ? "Inner exception: " + e.InnerException.Message
                    : null)));
            }
            finally
            {
                webView.Dispose();
            }
        }