【发布时间】:2020-02-16 18:41:23
【问题描述】:
Xamarin (Visual Studio 2017) 中的 WebView 有一点奇怪的问题
此方案有效:
我在“OnCreate”事件中将 html 内容放入 webview,webview 显示“Hello”
这种情况不起作用:
我在 OnCreate 中创建一个按钮控件并分配一个单击事件。 clickevent 触发所以这个工作。但是“Hello2”并没有放入 WebView,尽管我使用了完全相同的代码。
可能是什么问题?
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/LocalWebView">
</WebView>
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main); Android.Content.Context context = ApplicationContext;
Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
//This works and shows: "Hello"
WebView localWebView = FindViewById<WebView>(Resource.Id.LocalWebView);
localWebView.SetWebViewClient(new WebViewClient()); // stops request going to Web Browser
localWebView.LoadData("<html>Hello</html>", "text/html", "utf-8");
Button button = new Button(context);
button.Click += (sender, args) =>
{
//The click event fires but Hello2" is not put to the webview?
Button btn = sender as Button;
WebView localWebView2 = FindViewById<WebView>(Resource.Id.LocalWebView);
localWebView2.SetWebViewClient(new WebViewClient()); // stops request going to Web Browser
localWebView2.LoadData("<html>Hello2</html>", "text/html", "utf-8");
};
}
【问题讨论】:
标签: c# android xamarin webview