Sunday, July 29, 2018

Pinch Feature in WebView in Xamarin.forms



Introduction: 

     Hello friends,In this blog we work on WebView control and create custom renderer in each platform specification for enhancement a pinch feature.

Programming:

Core:

       In forms project,we will create our MyWebView class and inherit it from Xamarin.forms.WebView class.this is the WebView class of Xamarin technology.now our MyWebView have all the features and properties of Xamarin.forms.WebView.
using System;
namespace Demo.Renderer
{
public class myWebView:Xamarin.Forms.WebView
{
}
}

Xamarin.droid:

      Here,we create a custom Renderer in droid solution and Export this renderer to our custom WebView.
using System;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Webkit;
using Android.Widget;
using Demo.Droid;
using Demo.Renderer;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(myWebView), typeof(Demo.Droid.myWebViewRenderer))]
namespace Demo.Droid
{

public class myWebViewRenderer: WebViewRenderer
{
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (Control != null)
{
Control.Settings.BuiltInZoomControls = true;
Control.Settings.DisplayZoomControls = true;
}
base.OnElementPropertyChanged(sender, e);
}
}
}

Xamarin.ios:

      Here We create custom Renderer for Xamarin.ios.this cutom renderer uses the native webview for adding pinch feature.

using System;
using Demo.Renderer;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(myWebView), typeof(Demo.iOS.myWebViewRenderer))]
namespace Demo.iOS
{
class myWebViewRenderer:WebViewRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (NativeView != null && e.NewElement != null)
{
var webView = ((UIWebView)NativeView);
webView.ScalesPageToFit = true;

}
}
}
}

5 comments:

Implementation of Font awesome

Introduction     Hello All Today we will understand how to add icon or images with help of font  awesome.Font awesome is a font fa...