2017-08-01 9 views
1

Je veux utiliser le geste de pincement trop zoom avec le WPFExtensions.Controls.ZoomControl. En fonction de ce answer - je sais comment détecter le geste de pincement. Je l'utilise en tant que membre _detector. J'utilise ZoomControl.ManipulationDelta pour faire le zoom.WPFExtensions ZoomControl - Zoom pincé saute

Je pensais que je prends juste ManipulationDelta et zoomez comme ceci:

private void AssociatedObject_ManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e) 
{ 
    if (_detector.IsScalingAllowed) // this just tells me if it is a pinch gesture or not 
    { 
     if ((e.DeltaManipulation.Scale.X < 1) 
      || (e.DeltaManipulation.Scale.X > 1)) 
     { 
      //AssociatedObject is ZoomControl 
      AssociatedObject.Zoom = Math.Max(e.DeltaManipulation.Scale.X, e.DeltaManipulation.Scale.Y); 
     } 
    } 
} 

Mais ce sera juste faire mon ZoomControl zoom assez pauvres et des sauts en arrière.

Mon code complet ressemble à ceci:

public class ZoomBoxBehaviour : Behavior<ZoomControl> 
{ 
    private GestureDetector _detector; 

    protected override void OnAttached() 
    { 
     base.OnAttached(); 
     if (AssociatedObject != null) 
     { 
      _detector = new GestureDetector(AssociatedObject); 

      if (!AssociatedObject.IsManipulationEnabled) AssociatedObject.IsManipulationEnabled = true; 

      AssociatedObject.ManipulationDelta += AssociatedObject_ManipulationDelta; 
     } 
    } 

    protected override void OnDetaching() 
    { 
     if (AssociatedObject != null) 
     { 
      AssociatedObject.ManipulationDelta -= AssociatedObject_ManipulationDelta; 
     } 

     base.OnDetaching(); 
    } 

    private void AssociatedObject_ManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e) 
    { 
     if (_detector.IsScalingAllowed) // this just tells me if it is a pinch gesture or not 
     { 
      if ((e.DeltaManipulation.Scale.X < 1) 
       || (e.DeltaManipulation.Scale.X > 1)) 
      { 
       AssociatedObject.Zoom = Math.Max(e.DeltaManipulation.Scale.X, e.DeltaManipulation.Scale.Y); 
      } 
     } 
    } 
} 

Vous pouvez obtenir le GestureDetectorhere de la première réponse.

MISE À JOUR

je l'ai fait pour zoomer correctement au centre

private void AssociatedObject_ManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e) 
    { 
     if (_detector.IsScalingAllowed) // this just tells me if it is a pinch gesture or not 
     { 
      if ((e.DeltaManipulation.Scale.X < 1) 
       || (e.DeltaManipulation.Scale.X > 1)) 
      { 
       AssociatedObject.Zoom = Math.Max(e.CumulativeManipulation.Scale.X, e.CumulativeManipulation.Scale.Y); 

       //Size newSize = new Size(AssociatedObject.ZoomBox.Size.Width * e.CumulativeManipulation.Scale.X, 
       //      AssociatedObject.ZoomBox.Size.Height * e.CumulativeManipulation.Scale.Y); 

       //AssociatedObject.ZoomTo(new Rect(new Point(0,0), newSize)); 
      } 
     } 


    } 

Mais maintenant je veux Zoomez au centre de mon geste, mais a commenté code va juste zoomer pour max .... Pourquoi?

Répondre

0

Je l'ai résolu

public class ZoomBoxBehaviour : Behavior<ZoomControl> 
    { 
     private GestureDetector _detector; 
     protected override void OnAttached() 
     { 
      base.OnAttached(); 
      if (AssociatedObject != null) 
      { 
       _detector = new GestureDetector(AssociatedObject); 

       if (!AssociatedObject.IsManipulationEnabled) AssociatedObject.IsManipulationEnabled = true; 

       AssociatedObject.ManipulationDelta += AssociatedObject_ManipulationDelta; 
      } 
     } 


     protected override void OnDetaching() 
     { 
      if (AssociatedObject != null) 
      { 
       AssociatedObject.ManipulationDelta -= AssociatedObject_ManipulationDelta; 
      } 

      base.OnDetaching(); 
     } 

     private void AssociatedObject_ManipulationStarting(object sender, ManipulationStartingEventArgs e) 
     { 
      e.ManipulationContainer = ((FrameworkElement)e.Source).Parent as FrameworkElement; 
     } 

     private void AssociatedObject_ManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e) 
     { 
      if (_detector.IsPanningAllowed) 
      { 

       // Limit the X/Y translation extent to prevent the element from 'jumping' when using slow touchscreens, or many touch points. 
       const double translationThreshold = 100.0; 

       //Perform a translation(pan) tranformation 
       if ((e.DeltaManipulation.Translation.X < translationThreshold && 
        e.DeltaManipulation.Translation.X > -translationThreshold) 
       ) 
       { 
        AssociatedObject.TranslateX += e.DeltaManipulation.Translation.X; 
       } 

       if ((e.DeltaManipulation.Translation.Y < translationThreshold && 
        e.DeltaManipulation.Translation.Y > -translationThreshold)) 
       { 
        AssociatedObject.TranslateY += e.DeltaManipulation.Translation.Y; 
       } 
      } 

      if (_detector.IsScalingAllowed) // this just tells me if it is a pinch gesture or not 
      { 
       if ((e.CumulativeManipulation.Scale.X < 1) 
        || (e.CumulativeManipulation.Scale.X > 1)) 
       { 
        AssociatedObject.Zoom = Math.Max(e.CumulativeManipulation.Scale.X, e.CumulativeManipulation.Scale.Y); 
       } 
      } 

     } 

    }