【问题标题】:Trying to put a Html string to the WebView in Xamarin试图将 Html 字符串放入 Xamarin 中的 WebView
【发布时间】: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


    【解决方案1】:

    当我确实将宽度和高度放入 WebView 时,它现在可以工作了。 奇怪的是“fill_parent”或“match_parent”没有展开 WebView。

    <WebView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="500px"
      android:layout_height="1000px"
      android:id="@+id/LocalWebView">
    </WebView>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-22
      • 1970-01-01
      • 2016-01-31
      • 2012-05-10
      • 1970-01-01
      • 2017-08-08
      • 2014-04-27
      相关资源
      最近更新 更多