Xamarin.Forms gesture password implementation

Xamarin.Forms gesture password implementation

In the previous article, it was mentioned that Xamarin.Android, Xamarin.iOS, and UWP implement the gesture password function respectively. Now we implement this function in Xamarin.Forms.

  

The principle is the same as Xamarin.Android, Xamarin.iOS, UWP, the key is how to use ViewRenderer.

First we create a new Xamarin.Forms project:

Create a GuestureLockView in the project to inherit View (official document:  Custom Renderer ).

Next, create a GuestureLockViewRenderer in the Android, iOS, and UWP projects respectively

Android中 : GuestureLockViewRenderer继承ViewRenderer<GuestureLockView, Android.Views.View>

iOS中: GuestureLockViewRenderer继承ViewRenderer<GuestureLockView, UIView>

In UWP: GuestureLockViewRenderer inherits ViewRenderer<GuestureLockView, CanvasControl> , CanvasControl needs to install Win2D library in NuGet.

You also need to override the protected override void OnElementChanged(ElementChangedEventArgs<GuestureLockView> e) method in GuestureLockViewRenderer.

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

  if (Control == null) {
    // Instantiate the native control and assign it to the Control property with
    // the SetNativeControl method
  }

  if (e.OldElement != null) {
    // Unsubscribe from event handlers and cleanup any resources
  }

  if (e.NewElement != null) {
    // Configure the control and subscribe to event handlers
  }
}

This method is called when the control loads. The original text is as follows:

An overridden version of the OnElementChanged method, in each platform-specific renderer class, is the place to perform the native control instantiation and customization. The SetNativeControl method should be used to instantiate the native control, and this method will also assign the control reference to the Control property.

Then it is to draw graphics according to each platform in GuestureLockViewRenderer, so that this requirement can be achieved.

In this experiment, it happens that the Control is null (in UWP), then you need to call the SetNativeControl method to initialize a NativeControl.

 

if(Control == null)
{
    SetNativeControl(new CanvasControl());
}

 Github:https://github.com/devinZhou102/Plugin.GuestureLock

 Xamarin.Android gesture password: http://www.cnblogs.com/devin_zhou/p/8057243.html

 Xamarin.iOS gesture password: http://www.cnblogs.com/devin_zhou/p/8047313.html

Xamarin.UWP gesture password: http://www.cnblogs.com/devin_zhou/p/8052305.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325071431&siteId=291194637