【发布时间】:2017-04-05 04:12:19
【问题描述】:
在我的应用程序中,我应该插入一个 Markdown 渲染。我找到了这个组件http://thatcsharpguy.com/post/markdownview-xamarin-forms-control/ 并且渲染工作正常,但是我遇到了 WebView 的问题。如果我将某些东西传递给 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();
}
每个平台的实现
机器人
[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:///";
}
}
}
怎么了?提前感谢您的帮助。
【问题讨论】:
-
抛出异常的具体行是什么?
-
总是在这个代码之外。在 UWP 中 UnhandledException += (sender, e) => { if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break(); };
-
只有 UWP 会抛出异常?或者这只是您当前正在测试的平台?堆栈跟踪说明了什么?
-
在 iOS 中“System.Reflection.TargetInvocationException: 异常已被调用的目标抛出。”
-
查看 InnerException
标签: c# webview xamarin.ios xamarin.forms xamarin.uwp