Xamarin WebView 缩放以适应

jba*_*g10 4 xamarin xamarin.forms

有没有办法在 Xamarin 中设置 WebView 以默认缩放其内容以适应屏幕,并且仍然允许捏放大/缩小?

我们将使用它来显示我们在线的文档。

小智 6

我解决了自定义渲染器适合页面和缩放的比例,如下所示

IOS版

 public class CustomWebViewRenderer : WebViewRenderer
{
    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);

        var view = Element as CustomWebView;
        if (view == null || NativeView == null)
        {
            return;
        }
        this.ScalesPageToFit = true;
    }

}
Run Code Online (Sandbox Code Playgroud)

安卓版

public class CustomWebViewRenderer : WebViewRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {
            Control.Settings.BuiltInZoomControls = true;
            Control.Settings.DisplayZoomControls = false;

            Control.Settings.LoadWithOverviewMode = true;
            Control.Settings.UseWideViewPort = true;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


xer*_*ant -1

如果您使用的是 Xamarin.Forms,它看起来像这样

<?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="WebViewDemo.LoadingDemo" Title="Loading Demo">
    <ContentPage.Content>
    <StackLayout>
      <WebView x:Name="Browser"
      VerticalOptions = LayoutOptions.FillAndExpand ,
      HorizontalOptions = LayoutOptions.FillAndExpand,
    </StackLayout>
    </ContentPage.Content>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

这应该填满页面并且不影响捏合和缩放。