Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin表单Webview自定义渲染器缩放不工作_Xamarin_Webview_Zooming_Renderer - Fatal编程技术网

Xamarin表单Webview自定义渲染器缩放不工作

Xamarin表单Webview自定义渲染器缩放不工作,xamarin,webview,zooming,renderer,Xamarin,Webview,Zooming,Renderer,好的,我一直在尝试让自定义渲染器工作,但我有一个问题。 Xamarin论坛讨论如下: 问题是当我缩小时,网络视图停留在屏幕的一部分,不能适应整个屏幕 我添加了项目的代码: MyWebView.cs 使用制度; 使用System.Collections.Generic; 使用系统文本; 使用Xamarin.Forms; 名称空间CofarLE_ejempo_5 { 公共类MyWebView:WebView { 公共整数级 { 获取{return intGetValueZoomInLevelProp

好的,我一直在尝试让自定义渲染器工作,但我有一个问题。 Xamarin论坛讨论如下:

问题是当我缩小时,网络视图停留在屏幕的一部分,不能适应整个屏幕

我添加了项目的代码:

MyWebView.cs

使用制度; 使用System.Collections.Generic; 使用系统文本; 使用Xamarin.Forms; 名称空间CofarLE_ejempo_5 { 公共类MyWebView:WebView { 公共整数级 { 获取{return intGetValueZoomInLevelProperty;} set{SetValueZoomInLevelProperty,value;} } 公共布尔启用ZoomControl { 获取{return boolGetValueEnableZoomControlProperty;} set{SetValueEnableZoomControl属性,值;} } 公共静态只读BindableProperty ZoomInLevelProperty=BindableProperty.CreatepropertyName:ZoomInLevel,returnType:typeofint,declaringType:typeofMyWebView,默认值:100,propertyChanged:OnZoomInLevelPropertyChanged; 公共静态只读BindableProperty EnableZoomControl属性=BindableProperty.CreatepropertyName:EnableZoomControl,returnType:typeofbool,declaringType:typeofMyWebView,默认值:false,propertyChanged:OneEnableZoomChanged; 私有静态无效OnZoomInLevelPropertyChangedBindableObject可绑定、object oldValue、object newValue { var control1=MyWebViewbindable; control1.ZoomInLevel=intnewValue; } 私有静态void OnEnableZoomChangedBindableObject可绑定、object oldValue、object newValue { var control1=MyWebViewbindable; control1.EnableZoomControl=boolnewValue; } } } MyWebViewRendereriOS.cs

使用制度; 使用System.Collections.Generic; 使用系统组件模型; 使用System.Linq; 使用系统文本; 使用CofarLE_ejempo_5; 使用CofarLE_ejampo_5.iOS; 使用基础; 使用UIKit; 使用Xamarin.Forms; 使用Xamarin.Forms.Platform.iOS; [程序集:ExportRenderTypeOfMyWebView,TypeOfMyWebViewRenderIOS] 名称空间CofarLE_ejampo_5.iOS { 公共类MyDelegate:UIScrollViewDelegate { 公共UIView myView; 用于缩放的公共UIView视图; 公共MyDelegateUIView视图 { myView=视图; ViewForZoom=视图; } 用于缩放的公共覆盖UIView视图CrollView UIScrollView滚动视图 { MessagingCenter.Subscribethis,zoom,sender,arg=>{ 如果arg==true { myView.ContentMode=UIViewContentMode.ScaleToFill; ViewForZoom=myView; } 其他的 { ViewForZoom=null; } }; 返回视图进行缩放; } } 公共类MyWebViewRenderIOS:WebViewRenderer { 受保护的覆盖无效OnElementChangedVisualElementChangedEventArgs e { base.OnElementChangede; 如果NativeView!=null&&e.NewElement!=null { var control1=作为UIWebView的NativeView; 如果e.OldElement!=null { e、 OldElement.PropertyChanged-=OneElementPropertyChanged; } 如果e.NewElement!=null { e、 NewElement.PropertyChanged+=OneElementPropertyChanged; } control1.ScalesPageToFit=true; control1.ScrollView.Delegate=新的MyDelegatecontrol1; control1.ContentMode=UIViewContentMode.ScaleToFill; control1.ScrollView.SizeToFit; } } 私有void OnElementPropertyChangedobject发件人,PropertyChangedEventArgs e { var control1=作为UIWebView的NativeView; 如果control1==null { 回来 } control1.ScalesPageToFit=true; ScrollView.MaximumZoomScale=2; ScrollView.MinimumZoomScale=nfloat.Parse0.5; control1.ScrollView.MaximumZoomScale=2; control1.ScrollView.SizeToFit; control1.ScrollView.MinimumZoomScale=nfloat.Parse0.5; } } }
你可以试试这个,这是你需要的效果吗

1.创建自定义视图ZoomWebView:

二,。自定义渲染器ZoomWebViewRenderer

3.在页面中调用xaml:

<ContentPage.Content>
    <local:ZoomWebView Uri="https://www.microsoft.com" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
</ContentPage.Content>

谢谢你的回答。好的,但是它已经在安卓上运行了,我需要它iOS@PedroReglero很抱歉现在看到这个。ios问题解决了吗?
 public class ZoomWebViewRenderer : ViewRenderer<ZoomWebView, Android.Webkit.WebView>
 {
    Context _context;
    public ZoomWebViewRenderer(Context context) : base(context)
    {
        _context = context;
    }

    protected override void OnElementChanged(ElementChangedEventArgs<ZoomWebView> e)
    {
        base.OnElementChanged(e);

        if (Control == null)
        {
            var webView = new Android.Webkit.WebView(_context);
            webView.Settings.UseWideViewPort = true;
            webView.Settings.LoadWithOverviewMode = true;
            webView.Settings.SetSupportZoom(true);
            webView.Settings.BuiltInZoomControls = true;
            webView.Settings.DisplayZoomControls = false;
            webView.SetWebViewClient(new ZoomWebViewClient());
            SetNativeControl(webView);
            Control.LoadUrl(Element.Uri);
        }
    }

    class ZoomWebViewClient : WebViewClient
    {
        public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
        {
            view.LoadUrl(request.Url.ToString());
            return true;
        }
    }
}
<ContentPage.Content>
    <local:ZoomWebView Uri="https://www.microsoft.com" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
</ContentPage.Content>