c# - 在 ScrollViewer (UWP) 中移动图像

标签 c# uwp windows-10 winrt-xaml windows-10-mobile

我在 Scrollviewer 中有一个 Image...

<ScrollViewer x:Name="Scrollster" ZoomMode="Enabled" MinZoomFactor="1" MaxZoomFactor="4"
          HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" ManipulationMode="All">
    <Image x:Name="Img" Source="{x:Bind ImgSource}" Stretch="UniformToFill" PointerPressed="Img_PointerPressed"/>
</ScrollViewer>

我想在使用鼠标指针拖动图像时移动图像!

我试过:

private void Img_PointerPressed(object sender,PointerRoutedEventArgs e)
{
  var p = e.Pointer;
}

但我无法获得指针位置来更改滚动查看器的位置。

我的代码有什么问题?我做得对吗?

最佳答案

应该在 Img 控件上设置 ManipulationMode。此外,您可能希望指定所需的确切模式而不是 All 以防止不必要的手势处理。

<Image x:Name="Img" Source="{x:Bind ImgSource}" Width="150" Height="150" Stretch="UniformToFill" 
       ManipulationMode="TranslateX, TranslateY"
       ManipulationStarted="Img_ManipulationStarted"
       ManipulationDelta="Img_ManipulationDelta"
       ManipulationCompleted="Img_ManipulationCompleted">
    <Image.RenderTransform>
        <CompositeTransform x:Name="Transform" />
    </Image.RenderTransform>
</Image>

根据您上面的描述,我认为同时打开 TranslateXTranslateY 应该就足够了。然后,您将需要处理操作事件,例如 ManipulationStartedManipulationDeltaManipulationCompleted

您的大部分逻辑应该在 ManipulationDelta 事件中完成,该事件将在平移过程中多次触发。这是您获取 XY 位置并相应设置它们的地方。

这是一个简单的示例。

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;
}

关于c# - 在 ScrollViewer (UWP) 中移动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31896930/

相关文章:

c# - 图像控件绑定(bind)生成的Imageurl

c# - AddWithValue sql 注入(inject)安全吗?为什么?

c# - 策略工厂模式的 autofac 配置(已经解决)

c# - x :Type missing in UWP - How override base control style?

windows - 如何在不进入 Windows 10 BIOS 的情况下检查英特尔虚拟化是否已启用

c# - 只有一个 "message pump"还是很多?

c# - UWP Combobox SelectedItem 忽略其绑定(bind)值

c# - UWP 导航 View : How to resize the width?

windows-10 - 如何将焦点放在 Windows 10 UWP 应用程序中的网格上?

windows-10 - "minikube"集群不存在