Platform specific pages in xamarin forms

Create a Xamarin.Forms Project

After you open the visual studio, you need to select File > New Solution from the file menu.

Create a Xamarin.Forms page.

You need to select File > New Solution from the file menu.

<! — ?xml version=”1.0" encoding=”UTF-8"? →`
public partial class CameraPage : ContentPage
{
public CameraPage ()
{
InitializeComponent ();
}
}

Add a Button for the main Page to Open Camera.

When you open the default ContentPage, you will see following code on the xaml

<! — ?xml version=”1.0" encoding=”utf-8"? →
<label></label>
<! — ?xml version=”1.0" encoding=”utf-8"? →<label></label><button></button>
async void OnButtonClicked(object sender, EventArgs args)
{
await Navigation.PushAsync(new CameraPage());
}
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new CustomContentPagePage());
}

Creating the Page Renderer on each Platform

The process for creating the custom renderer class is as follows:

Creating the Page Renderer on iOS

[assembly: ExportRenderer(typeof(CameraPage), typeof(CameraPageRenderer))]
namespace CustomContentPage.iOS
{
public class CameraPageRenderer : PageRenderer
{
AVCaptureSession captureSession;
AVCaptureDeviceInput captureDeviceInput;
AVCaptureStillImageOutput stillImageOutput;
UIView liveCameraStream;
UIButton takePhotoButton;
UIButton toggleCameraButton;
UIButton toggleFlashButton;
protected override void OnElementChanged(VisualElementChangedEventArgs e){
base.OnElementChanged(e);
if (e.OldElement != null || Element == null){
return;
}
try{
SetupUserInterface();
SetupEventHandlers();
SetupLiveCameraStream();
AuthorizeCameraUse();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(@” ERROR: “, ex.Message);
}
}
}
}
…….

Creating the Page Renderer on droid

[assembly: ExportRenderer(typeof(CameraPage), typeof(CameraPageRenderer))]
namespace CustomRenderer.Droid
{
public class CameraPageRenderer : PageRenderer, TextureView.ISurfaceTextureListener
{

public CameraPageRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs e) {
base.OnElementChanged(e);
if (e.OldElement != null || Element == null) {
return;
}
try
{
SetupUserInterface();
SetupEventHandlers();
AddView(view);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(@” ERROR: “, ex.Message);
}
}

}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store