Hello,
Welcome to our Microsoft Q&A platform!
You can have a try with following code in PanControl.xaml.cs
, App.ScreenWidth
and App.ScreenHeight
are defined in APP.xaml.cs
, 50 is the height of RowDefinition
.
PanControl.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace PanInScrollView
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class PanControl : ContentView
{
public PanGestureRecognizer PanGestureRecognizer { get; set; }
public PanControl()
{
InitializeComponent();
PanGestureRecognizer = new PanGestureRecognizer
{
TouchPoints = 1
};
PanGestureRecognizer.PanUpdated += PanGestureRecognizer_PanUpdated;
GestureRecognizers.Add(PanGestureRecognizer);
}
private void PanGestureRecognizer_PanUpdated(object sender, PanUpdatedEventArgs e)
{
PanControl pan = (PanControl)sender;
switch (e.StatusType)
{
case GestureStatus.Running:
double a = pan.TranslationX + e.TotalX;
double b = pan.TranslationY + e.TotalY;
pan.TranslationX = a < 0 ? 0 : (a > (App.ScreenWidth - pan.Width) ? (App.ScreenWidth - pan.Width) : a);
pan.TranslationY = b < 0 ? 0 : (b > (App.ScreenHeight - pan.Height - 50) ? (App.ScreenHeight - pan.Height - 50) : b);
break;
case GestureStatus.Completed:
break;
}
}
}
}
MainActivity of Android
var width = Resources.DisplayMetrics.WidthPixels;
var height = Resources.DisplayMetrics.HeightPixels;
var density = Resources.DisplayMetrics.Density;
App.ScreenWidth = (width - 0.5f) / density;
App.ScreenHeight = (height - 0.5f) / density;
APP.xaml.cs
public static double ScreenWidth;
public static double ScreenHeight;
In addintion, your platform is Android( Device.RuntimePlatform == Device.Android
),you can have a try in iOS.
AppDelegate.cs
App.ScreenWidth = UIScreen.MainScreen.Bounds.Width;
App.ScreenHeight = UIScreen.MainScreen.Bounds.Height;
Best Regards,
Wenyan Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.