【问题标题】:Moving Image in ScrollViewer (UWP)ScrollViewer (UWP) 中的移动图像
【发布时间】:2015-08-08 17:59:04
【问题描述】:

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

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

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

【问题讨论】:

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


    【解决方案1】:

    应该在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;
    }
    

    【讨论】:

    • 工作正常,但现在在触摸模式下(桌面和移动)我不能捏放大和缩小!有什么问题?我应该在一个新主题中问它吗?
    • 我相信你需要打开ManipulationModes.Scale并手动处理this.Transform.ScaleXthis.Transform.ScaleY。或者,当有多个联系人时,禁用ManipulationMode,让系统处理放大/缩小。
    • 现在我知道如何修复它:将 System 添加到 ManipulationMode 将修复它。我的意思是:ManipulationMode="TranslateX, TranslateY, System"
    • 是的,因为默认模式是系统。因此,当我更改为 TranslateX n TranslateY 时,我禁用了所有系统自动处理。 :p
    • 谢谢。拯救了我的一天!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    相关资源
    最近更新 更多