Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

WebView errors

$
0
0

HI guy,
in my application I should insert a Markdown render. I found this component http://thatcsharpguy.com/post/markdownview-xamarin-forms-control/ and the render is working fine but I have a problem with the WebView. If I pass to a WebView something, it always goes in error and the error is:

Object reference not set to an instance of an object

Page1.xaml

     <?xml version="1.0" encoding="utf-8" ?>
     <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              x:Class="WordBankEasy.Views.Page1">
         <ContentPage.Content>
           <StackLayout>
             <WebView x:Name="Browser" VerticalOptions="FillAndExpand">
             </WebView>
           </StackLayout>
         </ContentPage.Content>
     </ContentPage>

Page1.xaml.cs

         public Page1()
         {
             InitializeComponent();

             var htmlSource = new HtmlWebViewSource();
             htmlSource.Html = @"<html><body>
     <h1>Xamarin.Forms</h1>
     <p>Welcome to WebView.</p>
     </body></html>";
             htmlSource.BaseUrl = DependencyService.Get<IBaseUrl>().Get();

             this.Browser.Source = htmlSource;
         }

I created my interface

     public interface IBaseUrl {
         string Get();
     }

and its implementations for each platform

Droid

 [assembly: Xamarin.Forms.Dependency(typeof(IBaseUrl))]
 namespace WordBankEasy.Android
 {
     public class BaseUrl_Android : IBaseUrl
     {
         public string Get()
         {
             return "file:///android_asset/";
         }
     }
}

iOS

 [assembly: Xamarin.Forms.Dependency(typeof(IBaseUrl))]
 namespace WordBankEasy.iOS
 {
     public class BaseUrl_iOS : IBaseUrl
     {
         public string Get()
         {
             return NSBundle.MainBundle.BundlePath;
         }
     }
 }

UWP

 [assembly: Xamarin.Forms.Dependency(typeof(IBaseUrl))]
 namespace WordBankEasy.UWP.Dependecies
 {
     public class BaseUrl_UWP : IBaseUrl
     {
         public string Get()
         {
             return "ms-appx-web:///";
         }
     }
 }

Viewing all articles
Browse latest Browse all 89864

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>