To be able to calculate a percentage of how far the user has dragged the image, we need to know the size of the control. This is not determined until the control is laid out by Xamarin.Forms.
We will override the OnSizeAllocated() method and add a _screenWidth field in the class to keep track of the current width of the window, by following these few steps:
- Open SwiperControl.xaml.cs.
- Add the following code to the file, putting the field at the beginning of the class and the OnSizeAllocated() method below the constructor:
private double _screenWidth = -1;
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
if (Application.Current.MainPage == null)
{
return;
}
_screenWidth = Application.Current.MainPage.Width;
}
The _screenWidth field is used to store the width as soon as we have resolved it. We do this by overriding the OnSizeAllocated() method that is called by...