0
votes

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="WBE.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 WBE.Android
{
    public class BaseUrl_Android : IBaseUrl
    {
        public string Get()
        {
            return "file:///android_asset/";
        }
    }
}

iOS

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

UWP

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

What is wrong? Thank you in advance for your help.

1
What specific line is throwing the exception?Jason
Always outside of this code. In UWP in UnhandledException += (sender, e) => { if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break(); };Enrico
only UWP throws an exception? Or is that just the platform you're currently testing? What does the stack trace say?Jason
In iOS "System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation."Enrico
look at the InnerExceptionJason

1 Answers

0
votes

thanks for your email. At the end I understood what the problem was. The error came from the Markdown component.