Xamarin.Forms UWP - 支持分页的打印 Webview
Xamarin.Forms UWP - Print Webview with pagination support
我正在研究基于 Xamarin.Forms 的项目,尝试使用分页打印 webview 内容。
我已经提到了以下 link:
How do I print WebView content in a Windows Store App?
但不幸的是,这种方式不适用于 Xamarin.Forms,因为上面 link 中演示的方式是使用 webviewbrush(矩形和webviewbrush 是 UWP 的平台相关控件)。问题是我们不能使用 webviewbrush 绘制矩形(Xamarin Forms 形状)。
作为解决方法,我执行了以下操作:
- 在 xamarin 表单中创建了一个 boxview PCL 项目
- 在 UWP 项目中为这个 boxview 创建了一个渲染器(这将为我们提供 windows 矩形形状),然后我将这个矩形形状放入 [=] 中的 public 静态属性之一49=] 项目的 App.cs class 使其可用于 Webviewrenderer.cs class 以使用 webviewbrush 填充此矩形。
- 我可以从 UWP 项目 webviewrenderer.cs class 访问它,但问题是打印机对话框显示空白页。
- 如上面的堆栈溢出所示,分页工作得很好link,但所有页面都是空的。
显然问题出在矩形上。因为如果我创建一个本机 UWP 项目并且打印机对话框也显示该网页,同样的逻辑也可以正常工作。
如有任何帮助,我们将不胜感激。
提前致谢!
Pagination works just fine as demonstrated in the above stack overflow link, but all pages are being empty.
我已经按照你的流程实现了基本的打印功能。我将 GetWebViewBrush
方法放在 webviewrenderer
中。不幸的是,同样的问题发生在我这边。
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;
}
我调试的时候发现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
方法中使用以下代码设置本机控件(Windows.UI.Xaml.Controls.WebView
)的Name
属性。
if (e.NewElement != null)
{
Control.Name = (Element as MyWebView).Name;
Control.NavigationCompleted += Control_NavigationCompleted;
}
令人惊讶的是,页面不再是空的。
我正在研究基于 Xamarin.Forms 的项目,尝试使用分页打印 webview 内容。
我已经提到了以下 link: How do I print WebView content in a Windows Store App?
但不幸的是,这种方式不适用于 Xamarin.Forms,因为上面 link 中演示的方式是使用 webviewbrush(矩形和webviewbrush 是 UWP 的平台相关控件)。问题是我们不能使用 webviewbrush 绘制矩形(Xamarin Forms 形状)。
作为解决方法,我执行了以下操作:
- 在 xamarin 表单中创建了一个 boxview PCL 项目
- 在 UWP 项目中为这个 boxview 创建了一个渲染器(这将为我们提供 windows 矩形形状),然后我将这个矩形形状放入 [=] 中的 public 静态属性之一49=] 项目的 App.cs class 使其可用于 Webviewrenderer.cs class 以使用 webviewbrush 填充此矩形。
- 我可以从 UWP 项目 webviewrenderer.cs class 访问它,但问题是打印机对话框显示空白页。
- 如上面的堆栈溢出所示,分页工作得很好link,但所有页面都是空的。
显然问题出在矩形上。因为如果我创建一个本机 UWP 项目并且打印机对话框也显示该网页,同样的逻辑也可以正常工作。
如有任何帮助,我们将不胜感激。
提前致谢!
Pagination works just fine as demonstrated in the above stack overflow link, but all pages are being empty.
我已经按照你的流程实现了基本的打印功能。我将 GetWebViewBrush
方法放在 webviewrenderer
中。不幸的是,同样的问题发生在我这边。
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;
}
我调试的时候发现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
方法中使用以下代码设置本机控件(Windows.UI.Xaml.Controls.WebView
)的Name
属性。
if (e.NewElement != null)
{
Control.Name = (Element as MyWebView).Name;
Control.NavigationCompleted += Control_NavigationCompleted;
}
令人惊讶的是,页面不再是空的。