Пример #1
0
        /// <summary>
        /// Initialize a new instance of <see cref="BookTextPageViewModel"/> class.
        /// </summary>
        /// <param name="book">EPUB book.</param>
        /// <param name="settings">The settings entity.</param>
        /// <param name="settingsRepository">The settings repository.</param>
        public BookTextPageViewModel(EpubBook book, SettingsEntity settings, ISettingsRepository settingsRepository)
        {
            this.settings           = settings;
            this.settingsRepository = settingsRepository;
            IFiler filer = DependencyService.Get <IFiler>();

            #region Custom HTML template

            HtmlDocument template = new HtmlDocument();
            // Hardcode filename
            Stream stream = filer.GetResourceFileStreamAsync("index.html").GetAwaiter().GetResult();
            template.Load(stream);

            var      script     = "<script>var lastPageNumber = " + this.settings.LastPage + "</script>";
            HtmlNode scriptNode = HtmlNode.CreateNode(script);

            var head = template.DocumentNode.ChildNodes.FirstOrDefault(c => c.Name == "html")
                       .ChildNodes.FirstOrDefault(f => f.Name == "head");

            head.ChildNodes.Add(scriptNode);

            var textContainer = template.DocumentNode.ChildNodes.FirstOrDefault(c => c.Name == "html")
                                .ChildNodes.FirstOrDefault(f => f.Name == "body")
                                .ChildNodes.FirstOrDefault(d => d.Id == "page");

            foreach (var html in book.Content.Html)
            {
                HtmlDocument document   = new HtmlDocument();
                string       htmlString = html.Value.Content.Replace(@"\", string.Empty);
                document.LoadHtml(htmlString);

                var body =
                    document.DocumentNode.ChildNodes.FirstOrDefault(c => c.Name == "html")
                    .ChildNodes.FirstOrDefault(f => f.Name == "body");

                foreach (var child in body.ChildNodes)
                {
                    textContainer.ChildNodes.Add(child);
                }
            }

            #endregion

            string htmlText = template.DocumentNode.OuterHtml.Replace(@"\", string.Empty);

            this.webView = new WebView
            {
                Source = new HtmlWebViewSource {
                    Html = htmlText
                },
                VerticalOptions   = LayoutOptions.FillAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand
            };

            this.webView.Navigating += this.NavigatingHandler;
            this.Content             = this.webView;
        }