Caz1224
Caz1224

Reputation: 1569

Xamarin Forms - Web View

Still trying to get my head around Xamarin Forms (iOS). I am working with the WorkingWithWebView template from the Xamarin Website which mostly works. But I am having trouble getting it to work when I remove the tab view off the App() class.

Tab view (This is what works)

        var tabs = new TabbedPage ();

        tabs.Children.Add (new LocalHtml {Title = "Local" });
        tabs.Children.Add (new LocalHtmlBaseUrl {Title = "BaseUrl" });
        tabs.Children.Add (new WebPage { Title = "Web Page"});
        tabs.Children.Add (new WebAppPage {Title ="External"});

        MainPage = tabs;

And when I go to LocalHtmlBaseUrl everything works as expected.

But what I am wanting to do is load a page straight away without having a tabbed navigation on the bottom.

So far I have tried to do the following:

        public App ()
        {
            //var tabs = new TabbedPage ();

            //tabs.Children.Add (new LocalHtml {Title = "Local" });
            //tabs.Children.Add (new LocalHtmlBaseUrl {Title = "BaseUrl" });
            //tabs.Children.Add (new WebPage { Title = "Web Page"});
            //tabs.Children.Add (new WebAppPage {Title ="External"});

            //MainPage = tabs;
            var parentpage = new Page();

            var page = new WebView();

            var browser = new BaseUrlWebView(); // temporarily use this so we can custom-render in iOS

            var htmlSource = new HtmlWebViewSource();

            htmlSource.Html = @"<html>
                                <head>
                                <link rel=""stylesheet"" href=""default.css"">
                                </head>
                                <body>
                                <h1>Xamarin.Forms</h1>
                                <p>The CSS and image are loaded from local files!</p>
                                <img src='XamarinLogo.png'/>
                                <p><a href=""index.html"">next page</a></p>
                                </body>
                                </html>";

            htmlSource.BaseUrl = DependencyService.Get<IBaseUrl>().Get();


            browser.Source = htmlSource;
            page = browser;
            parentpage = page;
            MainPage = page;

        }

At the parentpage = page - I get the error:

Cannot implicitly convert type 'Xamarin.Forms.WebView' to 'Xamarin.Forms.Page'

So the point of this thesis! I am just wanting to load that local html code that is specified in htmlSource.Html

Thanks

Caz

Upvotes: 0

Views: 570

Answers (1)

Jason
Jason

Reputation: 89204

your browser needs to be contained within a page

var browser = new BaseUrlWebView();

// do your other browser setup logic here

MainPage = new ContentPage { Content = browser };

Upvotes: 1

Related Questions