android webview 打印分页,Xamarin.Forms UWP - 打印Webview并提供分页支持

分页工作正常,如上面的堆栈溢出链接所示,但所有页面都是空的 .

我已根据您的流程实现了基本打印功能 . 我将 GetWebViewBrush 方法放在 webviewrenderer 中 . 不幸的是,同样的问题出现在我身边 .

async Task 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;

}

当我进行调试时,我发现 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 属性( Windows.UI.Xaml.Controls.WebView ) .

if (e.NewElement != null)

{

Control.Name = (Element as MyWebView).Name;

Control.NavigationCompleted += Control_NavigationCompleted;

}

令人惊讶的是,页面不再是空的 .

f104d208-a611-43f3-9e21-90bc1b562cc9.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值