Slider\ScrollViewer在触摸界面中无法正常工作

Bas*_*sem 7 wpf components slider multi-touch scrollviewer

在WPF中,我有以下XAML:

<ScrollViewer Canvas.Left="2266" Canvas.Top="428" Height="378" Name="scrollViewer1" Width="728" PanningMode="VerticalOnly" PanningRatio="2">
    <Canvas Height="1732.593" Width="507.667">
        <Slider Height="40.668" x:Name="slider1" Width="507.667" Style="{DynamicResource SliderStyle1}" Canvas.Left="15" Canvas.Top="150" />
        </Slider>
    </Canvas>
</ScrollViewer>
Run Code Online (Sandbox Code Playgroud)

这是一个包含Slider的ScrollViewer.我在触摸屏上使用以下内容,我甚至使用平移来垂直滚动ScrollViewer.设置PanningMode ="VerticalOnly"时,滑块停止工作!

我假设ScollViewer正在使用touch\slide事件并在滑块之前处理它(但我认为我在这方面错了).

这有什么解决方法吗?

Val*_*rie 11

我刚刚在我们的应用中解决了这个问题.

发生的事情是ScrollViewer在其PreviewTouchMove处理程序中捕获TouchDevice,该处理程序从其他控件"窃取"TouchDevice并阻止它们接收任何PreviewTouchMove或TouchMove事件.

为了解决这个问题,您需要实现一个自定义Thumb控件,该控件在PreviewTouchDown事件中捕获TouchDevice并存储对它的引用,直到PreviewTouchUp事件发生.然后,控件可以在适当的时候在其LostTouchCapture处理程序中"窃取"捕获.这是一些简短的代码:

public class CustomThumb : Thumb
{
    private TouchDevice currentDevice = null;

    protected override void OnPreviewTouchDown(TouchEventArgs e)
    {
        // Release any previous capture
        ReleaseCurrentDevice();
        // Capture the new touch
        CaptureCurrentDevice(e);
    }

    protected override void OnPreviewTouchUp(TouchEventArgs e)
    {
        ReleaseCurrentDevice();
    }

    protected override void OnLostTouchCapture(TouchEventArgs e)
    {
        // Only re-capture if the reference is not null
        // This way we avoid re-capturing after calling ReleaseCurrentDevice()
        if (currentDevice != null)
        {
            CaptureCurrentDevice(e);
        }
    }

    private void ReleaseCurrentDevice()
    {
        if (currentDevice != null)
        {
            // Set the reference to null so that we don't re-capture in the OnLostTouchCapture() method
            var temp = currentDevice;
            currentDevice = null;
            ReleaseTouchCapture(temp);
        }
    }

    private void CaptureCurrentDevice(TouchEventArgs e)
    {
        bool gotTouch = CaptureTouch(e.TouchDevice);
        if (gotTouch)
        {
            currentDevice = e.TouchDevice;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您需要重新模板Slider以使用CustomThumb而不是默认的Thumb控件.

  • 我还没有测试过这种方法,但它看起来足够有效.我会选择它作为答案.我解决所有问题的方法是使用Microsoft Surface 2.0 SDK http://www.microsoft.com/download/en/details.aspx?id=26716并使用其库中的ScrollViewer(处理上述所有问题) ). (2认同)