触摸界面中的 Slider ScrollViewer 无法正常工作 [英] Slider ScrollViewer in a touch interface not working properly

查看:20
本文介绍了触摸界面中的 Slider ScrollViewer 无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WPF 中,我有以下 XAML:

In WPF I've got the following 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>

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

It's a ScrollViewer containing a Slider. I'm using the following on a touch-screen, and I'm using the panning even to scroll the ScrollViewer vertically. When PanningMode="VerticalOnly" is set, the slider stops working!

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

I'm assuming the ScollViewer is consuming the touchslide event and handling it before the slider does (but I think I'm wrong on this front).

有什么解决方法吗?

推荐答案

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

I just solved this issue in our app.

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

What is happening is that the ScrollViewer captures the TouchDevice in its PreviewTouchMove handler, which "steals" the TouchDevice from other controls and prevents them from receiving any PreviewTouchMove or TouchMove events.

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

In order to work around this, you need to implement a custom Thumb control that captures the TouchDevice in the PreviewTouchDown event and stores a reference to it until the PreviewTouchUp event occurs. Then the control can "steal" the capture back in its LostTouchCapture handler, when appropriate. Here is some brief code:

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

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

Then you will need to re-template the Slider to use the CustomThumb instead of the default Thumb control.

这篇关于触摸界面中的 Slider ScrollViewer 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆