2017-09-13 1 views
1

저는 Xamarin.Forms 기반 프로젝트에서 페이지 매김을 사용하여 웹보기 내용을 인쇄하려고합니다. 위의 링크에서 설명하는 방식이 (webviewbrush를 사용하여 사각형 (창 모양)를 작성하여 있기 때문에 Xamarin.Forms와 How do I print WebView content in a Windows Store App?Xamarin.Forms UWP - 페이지 매김 지원으로 웹보기 인쇄

그러나 불행하게도이 방법은 작동하지 않습니다 :

나는 다음과 같은 이미 링크를 참조했습니다 사각형과 웹 뷰 브러시는 모두 UWP에 대한 플랫폼 종속 컨트롤입니다. 문제는 사각형을 그릴 때 webviewbrush를 사용할 수 없다는 것입니다 (Xamarin Forms 모양).

  • 는 자 마린 형태에서의 BoxView에게
  • 는 UWP 프로젝트에서이의 BoxView에 대한 렌더러 (이 우리에게 창 사각형 모양을 줄 것이다)를 생성 PCL 프로젝트를 생성 한 후 : 해결 방법으로

    나는 다음과 같은했다 이 직사각형 모양을 PCL 프로젝트의 App.cs 클래스에있는 public static 속성 중 하나에 넣어서이 사각형에 webviewbrush를 채우는 Webviewrenderer.cs 클래스에 사용할 수 있도록했습니다.
  • UWP 프로젝트에서 webviewrenderer.cs 클래스에서 액세스 할 수 있지만 문제는 프린터 대화 상자에 빈 페이지가 표시된다는 것입니다.
  • 페이지 매김은 위의 스택 오버플로 링크에서 설명한 것처럼 잘 작동하지만 모든 페이지가 비어 있습니다.

분명히 문제는 직사각형 일 것입니다. 네이티브 UWP 프로젝트를 만들고 프린터 대화 상자에 웹 페이지가 표시되는 경우에도 동일한 논리가 올바르게 작동하기 때문입니다.

도움을 주시면 감사하겠습니다.

미리 감사드립니다.

답변

0

페이지 매김은 위의 스택 오버플로 링크에서 설명한 것처럼 잘 작동하지만 모든 페이지는 비어 있습니다.

귀하의 프로세스에 따라 기본 인쇄 기능을 실현했습니다. webviewrendererGetWebViewBrush 메서드를 넣었습니다. 불행히도, 내 문제는 같은 문제가 발생합니다.

async Task<WebViewBrush> GetWebViewBrush(Windows.UI.Xaml.Controls.WebView webView) 
{ 
    // resize width to content 
    var _OriginalWidth = webView.Width; 
    var _WidthString = await webView.InvokeScriptAsync("eval", 
     new[] { "document.body.scrollWidth.toString()" }); 
    int _ContentWidth; 

    if (!int.TryParse(_WidthString, out _ContentWidth)) 
     throw new Exception(string.Format("failure/width:{0}", _WidthString)); 

    webView.Width = _ContentWidth; 

    // resize height to content 
    var _OriginalHeight = webView.Height; 

    var _HeightString = await webView.InvokeScriptAsync("eval", 
     new[] { "document.body.scrollHeight.toString()" }); 
    int _ContentHeight; 

    if (!int.TryParse(_HeightString, out _ContentHeight)) 
     throw new Exception(string.Format("failure/height:{0}", _HeightString)); 

    webView.Height = _ContentHeight; 

    // create brush 
    var _OriginalVisibilty = webView.Visibility; 

    webView.Visibility = Windows.UI.Xaml.Visibility.Visible; 

    var _Brush = new WebViewBrush 
    { 
     Stretch = Stretch.Uniform, 
     SourceName = webView.Name 
    }; 
    // _Brush.SetSource(webView); 

    _Brush.Redraw(); 

    // reset, return 
    webView.Width = _OriginalWidth; 
    webView.Height = _OriginalHeight; 
    webView.Visibility = _OriginalVisibilty; 
    return _Brush; 
} 

내가 debuging 때, 나는 webView.Name이 값을 설정하지, 그래서 나는 customWebView에 대한 새로운 Name 특성을 한 적이 발견했다.

public static readonly BindableProperty NameProperty = BindableProperty.Create(
propertyName: "Name", 
returnType: typeof(string), 
declaringType: typeof(MyWebView), 
defaultValue: default(string)); 

    public string Name 
    { 
     get { return (string)GetValue(NameProperty); } 
     set { SetValue(NameProperty, value); } 
    } 

} 

그리고는 OnElementChanged 방법에 다음 코드와 네이티브 제어()의 Name 속성을 설정합니다.

if (e.NewElement != null) 
{ 
    Control.Name = (Element as MyWebView).Name; 
    Control.NavigationCompleted += Control_NavigationCompleted; 
} 

놀랍게도이 페이지는 더 이상 비어 있지 않습니다. enter image description here

+0

멋진! 이것은 매력처럼 작동합니다! 고마워요 :) –

+0

@ 스 캐빈 저 : 고마워, 해냈어! –