Exemplo n.º 1
0
        private async void NavigationCompleteB(Windows.UI.Xaml.Controls.WebView webView, WebViewNavigationCompletedEventArgs args)
        {
            IsMainPageChild(webView);

            using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
            {
                System.Diagnostics.Debug.WriteLine("B webView.Size=[" + webView.Width + "," + webView.Height + "] IsOnMainThread=[" + P42.Utils.Environment.IsOnMainThread + "]");
                try
                {
                    //await Task.Delay(2000);
                    await webView.CapturePreviewToStreamAsync(ms);

                    var decoder = await BitmapDecoder.CreateAsync(ms);

                    var transform = new BitmapTransform
                    {
                        ScaledHeight = (uint)decoder.PixelHeight,
                        ScaledWidth  = (uint)decoder.PixelWidth
                    };

                    var pixelData = await decoder.GetPixelDataAsync(
                        BitmapPixelFormat.Bgra8,
                        BitmapAlphaMode.Straight,
                        transform,
                        ExifOrientationMode.RespectExifOrientation,
                        ColorManagementMode.DoNotColorManage);

                    var bytes = pixelData.DetachPixelData();


                    var piclib   = Windows.Storage.ApplicationData.Current.TemporaryFolder;
                    var fileName = (string)webView.GetValue(PngFileNameProperty) + ".png";
                    var file     = await piclib.CreateFileAsync(fileName, Windows.Storage.CreationCollisionOption.GenerateUniqueName);

                    using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);

                        encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                             BitmapAlphaMode.Ignore,
                                             (uint)decoder.PixelWidth, (uint)decoder.PixelHeight,
                                             0, 0, bytes);
                        await encoder.FlushAsync();
                    }

                    var onComplete = (TaskCompletionSource <ToFileResult>)webView.GetValue(TaskCompletionSourceProperty);
                    System.Diagnostics.Debug.WriteLine(GetType() + "." + P42.Utils.ReflectionExtensions.CallerMemberName() + ": Complete[" + file.Path + "]");
                    onComplete?.SetResult(new ToFileResult(false, file.Path));
                }
                catch (Exception e)
                {
                    var onComplete = (TaskCompletionSource <ToFileResult>)webView.GetValue(TaskCompletionSourceProperty);
                    onComplete?.SetResult(new ToFileResult(true, e.InnerException?.Message ?? e.Message));
                }
            }
        }
Exemplo n.º 2
0
        private async void NavigationCompleteA(Windows.UI.Xaml.Controls.WebView webView, WebViewNavigationCompletedEventArgs args)
        {
            //IsMainPageChild(webView);

            webView.NavigationCompleted -= NavigationCompleteA;
            var contentSize = await webView.WebViewContentSizeAsync();

            System.Diagnostics.Debug.WriteLine("A contentSize=[" + contentSize + "]");
            System.Diagnostics.Debug.WriteLine("A webView.Size=[" + webView.Width + "," + webView.Height + "] IsOnMainThread=[" + P42.Utils.Environment.IsOnMainThread + "]");

            //webView.Width = contentSize.Width;
            var width = (int)webView.GetValue(PngWidthProperty);

            webView.Width  = width;
            webView.Height = contentSize.Height;

            //webView.UpdateLayout();

            webView.NavigationCompleted += NavigationCompleteB;

            //if (webView.GetValue(HtmlStringProperty) is string html)
            //	webView.NavigateToString(html);
            //else
            //NavigationCompleteB(webView, args);
            webView.Refresh();
        }
Exemplo n.º 3
0
 public static string GetHtmlSource(WebView view)
 {
     if (null == view)
     {
         throw new ArgumentNullException("view");
     }
     return (string)view.GetValue(HtmlSourceProperty);
 }
Exemplo n.º 4
0
        private async void NavigationCompleteB(Windows.UI.Xaml.Controls.WebView webView, WebViewNavigationCompletedEventArgs args)
        {
            System.Diagnostics.Debug.WriteLine("B webView.Size=[" + webView.Width + "," + webView.Height + "]");


            using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
            {
                await webView.CapturePreviewToStreamAsync(ms);

                var decoder = await BitmapDecoder.CreateAsync(ms);

                var transform = new BitmapTransform
                {
                    ScaledHeight = (uint)decoder.PixelHeight,
                    ScaledWidth  = (uint)decoder.PixelWidth
                };

                var pixelData = await decoder.GetPixelDataAsync(
                    BitmapPixelFormat.Bgra8,
                    BitmapAlphaMode.Straight,
                    transform,
                    ExifOrientationMode.RespectExifOrientation,
                    ColorManagementMode.DoNotColorManage);

                var bytes = pixelData.DetachPixelData();


                var piclib   = Windows.Storage.ApplicationData.Current.TemporaryFolder;
                var fileName = (string)webView.GetValue(PngFileNameProperty) + ".png";
                var file     = await piclib.CreateFileAsync(fileName, Windows.Storage.CreationCollisionOption.GenerateUniqueName);

                using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);

                    encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                         BitmapAlphaMode.Ignore,
                                         (uint)decoder.PixelWidth, (uint)decoder.PixelHeight,
                                         0, 0, bytes);
                    await encoder.FlushAsync();
                }

                var onComplete = (Action <string>)webView.GetValue(OnCompleteProperty);
                onComplete?.Invoke(file.Path);
            }
        }
Exemplo n.º 5
0
 private void NavigationCompleteC(Windows.UI.Xaml.Controls.WebView webView, WebViewNavigationCompletedEventArgs args)
 {
     webView.NavigationCompleted -= NavigationCompleteC;
     if (webView.GetValue(TaskCompletionSourceProperty) is TaskCompletionSource <ToFileResult> onComplete)
     {
         if (webView.GetValue(ToFileResultProperty) is ToFileResult result)
         {
             System.Diagnostics.Debug.WriteLine(GetType() + "." + P42.Utils.ReflectionExtensions.CallerMemberName() + ": Complete[" + result.Result + "]");
             onComplete.SetResult(result);
         }
         else
         {
             onComplete.SetResult(new ToFileResult(true, "unknown error generating PNG."));
         }
     }
     else
     {
         throw new Exception("Failed to get TaskCompletionSource for UWP WebView.ToPngAsync");
     }
 }
Exemplo n.º 6
0
        private async void NavigationCompleteA(Windows.UI.Xaml.Controls.WebView webView, WebViewNavigationCompletedEventArgs args)
        {
            var contentSize = await webView.WebViewContentSizeAsync();

            webView.NavigationCompleted -= NavigationCompleteA;

            System.Diagnostics.Debug.WriteLine("A contentSize=[" + contentSize + "]");
            System.Diagnostics.Debug.WriteLine("A webView.Size=[" + webView.Width + "," + webView.Height + "]");

            webView.Width  = contentSize.Width;
            webView.Height = contentSize.Height;
            webView.UpdateLayout();

            webView.NavigationCompleted += NavigationCompleteB;

            var html = (string)webView.GetValue(HtmlStringProperty);

            webView.NavigateToString(html);
        }
Exemplo n.º 7
0
 public static string GetHtmlContent(WebView attached)
 {
     return (string)attached.GetValue(HtmlContentProperty);
 }
Exemplo n.º 8
0
 /// <summary>
 /// Gets source string
 /// </summary>
 /// <param name="obj">Webview</param>
 /// <returns>Source string</returns>
 public static string GetSourceString(_WebView obj)
 {
     return((string)obj.GetValue(SourceStringProperty));
 }
Exemplo n.º 9
0
 public static string GetUriSource(WebView view)
 {
     return (string)view.GetValue(UriSourceProperty);
 }
 public static string GetHtml(WebView obj)
 {
     return (string)obj.GetValue(HtmlProperty);
 }
Exemplo n.º 11
0
 /// <summary> 
 /// Gets a value indicating whether this is a looping FlipView 
 /// </summary> 
 /// <param name="obj">the flipView</param> 
 /// <returns>true if the list loops</returns> 
 public static string GetStringContent(WebView obj)
 {
     return (string)obj.GetValue(StringContentProperty);
 }
Exemplo n.º 12
0
 /// <summary> 
 /// Gets a value indicating whether this is a looping FlipView 
 /// </summary> 
 /// <param name="obj">the flipView</param> 
 /// <returns>true if the list loops</returns> 
 public static string GetSourceBinding(WebView obj)
 {
     return (string)obj.GetValue(SourceBindingProperty);
 }
Exemplo n.º 13
0
        private async void NavigationCompleteB(Windows.UI.Xaml.Controls.WebView webView, WebViewNavigationCompletedEventArgs args)
        {
            webView.NavigationCompleted -= NavigationCompleteB;
            webView.NavigationCompleted += NavigationCompleteC;

            IsMainPageChild(webView);

            using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
            {
                System.Diagnostics.Debug.WriteLine("B webView.Size=[" + webView.Width + "," + webView.Height + "] IsOnMainThread=[" + P42.Utils.Environment.IsOnMainThread + "]");
                try
                {
                    var width = (int)webView.GetValue(PngWidthProperty);
                    System.Diagnostics.Debug.WriteLine("B width=[" + width + "]");

                    var contentSize = await webView.WebViewContentSizeAsync();

                    System.Diagnostics.Debug.WriteLine("B contentSize=[" + contentSize + "]");
                    System.Diagnostics.Debug.WriteLine("B webView.Size=[" + webView.Width + "," + webView.Height + "] IsOnMainThread=[" + P42.Utils.Environment.IsOnMainThread + "]");

                    if (contentSize.Height != webView.Height || width != webView.Width)
                    {
                        webView.Width  = contentSize.Width;
                        webView.Height = contentSize.Height;
                        System.Diagnostics.Debug.WriteLine("B webView.Size=[" + webView.Width + "," + webView.Height + "] IsOnMainThread=[" + P42.Utils.Environment.IsOnMainThread + "]");

                        webView.InvalidateMeasure();
                        System.Diagnostics.Debug.WriteLine("B webView.Size=[" + webView.Width + "," + webView.Height + "] IsOnMainThread=[" + P42.Utils.Environment.IsOnMainThread + "]");
                    }

                    //await Task.Delay(2000);
                    await webView.CapturePreviewToStreamAsync(ms);

                    var decoder = await BitmapDecoder.CreateAsync(ms);

                    var transform = new BitmapTransform
                    {
                        //ScaledHeight = (uint)decoder.PixelHeight,
                        //ScaledWidth = (uint)decoder.PixelWidth
                        ScaledHeight = (uint)Math.Ceiling(webView.Height),
                        ScaledWidth  = (uint)Math.Ceiling(webView.Width)
                    };

                    var pixelData = await decoder.GetPixelDataAsync(
                        BitmapPixelFormat.Bgra8,
                        BitmapAlphaMode.Straight,
                        transform,
                        ExifOrientationMode.RespectExifOrientation,
                        ColorManagementMode.DoNotColorManage);

                    var bytes = pixelData.DetachPixelData();


                    var piclib   = Windows.Storage.ApplicationData.Current.TemporaryFolder;
                    var fileName = (string)webView.GetValue(PngFileNameProperty) + ".png";
                    var file     = await piclib.CreateFileAsync(fileName, Windows.Storage.CreationCollisionOption.GenerateUniqueName);

                    using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);

                        encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                             BitmapAlphaMode.Ignore,
                                             //(uint)decoder.PixelWidth, (uint)decoder.PixelHeight,
                                             (uint)Math.Ceiling(webView.Width), (uint)Math.Ceiling(webView.Height),
                                             0, 0, bytes);
                        await encoder.FlushAsync();
                    }

                    webView.Width  = (int)webView.GetValue(BeforeWidthProperty);
                    webView.Height = (int)webView.GetValue(BeforeHeightProperty);
                    webView.Refresh();

                    var toFileResult = new ToFileResult(false, file.Path);
                    webView.SetValue(ToFileResultProperty, toFileResult);
                }
                catch (Exception e)
                {
                    webView.Width  = (int)webView.GetValue(BeforeWidthProperty);
                    webView.Height = (int)webView.GetValue(BeforeHeightProperty);
                    webView.Refresh();

                    var toFileResult = new ToFileResult(true, e.InnerException?.Message ?? e.Message);
                    webView.SetValue(ToFileResultProperty, toFileResult);
                }
            }
            webView.Refresh();
        }