IEnumerable<FrameworkElement> GetWebPages(WebView webView, Windows.Foundation.Size page)
{
// ask the content its width
var widthString = webView.InvokeScript("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;
// ask the content its height
var heightString = webView.InvokeScript("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;
// how many pages will there be?
var scale = page.Width / contentWidth;
var scaledHeight = (contentHeight * scale);
var pageCount = (double)scaledHeight / page.Height;
pageCount = pageCount + ((pageCount > (int)pageCount) ? 1 : 0);
// create the pages
var pages = new List<Windows.UI.Xaml.Shapes.Rectangle>();
for (int i = 0; i < (int)pageCount; i++)
{
var translateY = -page.Height * i;
var _page = new Windows.UI.Xaml.Shapes.Rectangle
{
Height = page.Height,
Width = page.Width,
Margin = new Thickness(5),
Tag = new TranslateTransform { Y = translateY },
};
_page.Loaded += (s, e) =>
{
var rectangle = s as Windows.UI.Xaml.Shapes.Rectangle;
var brush = GetWebViewBrush(webView);
brush.Stretch = Stretch.UniformToFill;
brush.AlignmentY = AlignmentY.Top;
brush.Transform = rectangle.Tag as TranslateTransform;
rectangle.Fill = brush;
};
pages.Add(_page);
}
return pages;
}