xamarin Web视图无法滚动

ukqbszuj  于 2022-12-07  发布在  其他
关注(0)|答案(4)|浏览(162)

我有这个Xamarin表单页面:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:TestApp1"
             x:Class="TestApp1.MainPage">
<ContentPage.Content>
  <StackLayout Orientation="Vertical">
    <WebView Source="http://www.google.de" HeightRequest="3000" WidthRequest="100"/>
  </StackLayout>
</ContentPage.Content>
</ContentPage>

当我打开我的应用程序,在谷歌提示输入任何东西,我不能在结果页面上滚动。我如何启用这一点?
当我在谷歌上搜索"xamarin webview启用滚动"时,我只找到了关于禁用它的信息...

3b6akqbq

3b6akqbq1#

创建您自己的渲染器并重写此方法,如下所示:

public override bool DispatchTouchEvent(MotionEvent e)
{
    Parent.RequestDisallowInterceptTouchEvent(true);
    return base.DispatchTouchEvent(e);
}

请在此链接中查找详细信息

r9f1avp5

r9f1avp52#

<?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="ButtonRendererDemo.WebViewPage">

  <ContentPage.Content>
    <StackLayout Orientation="Vertical" Padding="10" Spacing="10">
      <Button Text="Press me" HorizontalOptions="CenterAndExpand" VerticalOptions="Center" BackgroundColor="Green"/>        
      <WebView Source="http://www.google.de" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
    </StackLayout>
  </ContentPage.Content>    
</ContentPage>

enxuqcxy

enxuqcxy3#

对我来说,问题出在<WebView/>内部,我设置了静态的HeightRequest和WidthRequest -因为文档中说如果使用<StackLayout>,这些是必需的-但事实并非如此
删除这些并添加VerticalOptions=“FillAndExpand”HorizontalOptions=“FillAndExpand”,为我解决了这个问题,并允许我按预期滚动。

<WebView VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Source="https://google.com/"/>
jckbn6z7

jckbn6z74#

这对我在android中非常有效

[assembly: ExportRenderer(typeof(MyWebView),typeof(MyWebViewRenderer))]
namespace App.Droid.Renderers
{
    class MyWebViewRenderer : WebViewRenderer
    {
        public MyWebViewRenderer(Context context) : base(context)
        {
        }
 
        protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
        {
            base.OnElementChanged(e);
        }
 
        public override bool DispatchTouchEvent(MotionEvent e)
        {
            Parent.RequestDisallowInterceptTouchEvent(true);
            return base.DispatchTouchEvent(e);
        }
    }
}

相关问题