I am developing a Windows phone app, where I have a viewportcontroller, that enables me to zoom in and out on content. I want to center the zoom at the point where I zoom. Whic
I tried a few different approaches and this one came out to be the smoothest. It's not pretty, but it works.
public partial class MainPage : PhoneApplicationPage
{
private const int MoveCount = 25;
private double _tickX;
private double _tickY;
private int _adjustCount = MoveCount+1;
public MainPage()
{
InitializeComponent();
Viewport.ViewportChanged += ViewportOnViewportChanged;
}
private void ViewportOnViewportChanged(object sender, ViewportChangedEventArgs viewportChangedEventArgs)
{
AdjstViewport();
}
private void AdjstViewport()
{
if (_adjustCount >= MoveCount) return;
_adjustCount++;
Viewport.SetViewportOrigin(new Point(Viewport.Viewport.X + _tickX, Viewport.Viewport.Y + _tickY));
}
private async void OnButtonClick(object sender, System.Windows.RoutedEventArgs e)
{
_adjustCount = 0;
var content = (FrameworkElement)Viewport.Content;
double zoomOriginX = (content.ActualWidth / 2) - (Viewport.Viewport.Width / 2);
double zoomOriginY = (content.ActualHeight / 2) - (Viewport.Viewport.Height / 2);
double distanceX = zoomOriginX - Viewport.Viewport.X;
double distanceY = zoomOriginY - Viewport.Viewport.Y;
_tickX = distanceX / MoveCount;
_tickY = distanceY / MoveCount;
AdjstViewport();
}
}