在ScrollViewer(UWP)中移动图像
我在Scrollviewer
有一张Image
……
当我用鼠标指针拖动图像时,我想移动图像!
我试过了:
private void Img_PointerPressed(object sender,PointerRoutedEventArgs e) { var p = e.Pointer; }
但我无法获得指针位置来改变scrollviewer位置。
我的代码出了什么问题? 我做得对吗?
应该在Img
控件上设置ManipulationMode
。 此外,您可能希望指定所需的确切模式,而不是All
以防止不必要的手势处理。
从上面的描述中,我认为启用TranslateX
和TranslateY
就足够了。 然后,您将需要处理ManipulationStarted
, ManipulationDelta
和ManipulationCompleted
等操作事件。
你的大多数逻辑都应该在ManipulationDelta
事件中完成,这将在平移过程中多次触发。 这是你获得X
和Y
位置并相应地设置它们的地方。
这是一个简单的示例。
void Img_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e) { // dim the image while panning this.Img.Opacity = 0.4; } void Img_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e) { this.Transform.TranslateX += e.Delta.Translation.X; this.Transform.TranslateY += e.Delta.Translation.Y; } void Img_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e) { // reset the Opacity this.Img.Opacity = 1; }