2016-11-21 115 views
0

在我的應用程序中,我應該插入一個Markdown渲染。我發現這個組件http://thatcsharpguy.com/post/markdownview-xamarin-forms-control/和渲染工作正常,但我有一個問題,WebView。如果我通過一個web視圖的東西,它總是轉到錯誤和錯誤是:Xamarin Forms WebView錯誤

對象引用不設置到對象的實例

的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; 
} 

我創建了接口

public interface IBaseUrl { 
    string Get(); 
} 

及其每個平臺

的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:///"; 
     } 
    } 
} 

有什麼不對?預先感謝您的幫助。

+0

什麼特定的行引發異常? – Jason

+0

始終在此代碼之外。在UnhandledException中的UWP + =(sender,e)=> if(global :: System.Diagnostics.Debugger.IsAttached)global :: System.Diagnostics.Debugger.Break(); }; – Enrico

+0

只有UWP引發異常?或者,那只是你正在測試的平臺?堆棧跟蹤說什麼? – Jason

回答

0

感謝您的電子郵件。最後我明白了問題所在。該錯誤來自Markdown組件。