在Slider控件中访问Thumb.DragCompleted

时间:2013-01-13 21:50:04

标签: windows-runtime winrt-xaml

我有以下代码,用于控制MediaElement控件的媒体位置。

<Slider Grid.Row="1" Maximum="{Binding TotalTimeStamp}" Value="{Binding CurrentTimeStamp, Mode=TwoWay}" Thumb.DragStarted="Thumb.OnDragStarted" Thumb.DragCompleted=""Thumb_OnDragCompleted/>

为了在MediaElement中寻找视频位置,我需要能够在拇指拖动开始时暂停视频,然后在完成拇指拖动后恢复视频。但是,出于某种原因,我无法在滑块控件中访问Thumb控件的DragStarted和DragCompleted,也无法访问拇指控件,即使潜入Slider模板显示它确实使用了滑块控件。

说这个,WPF完全可以实现。

1 个答案:

答案 0 :(得分:0)

我玩了一些模板,但看起来你是对的 - 如果你拖动拇指 - 你得到拖动事件,但如果你开始拖动其他地方 - 你可以得到的最好的是一些指针事件。也许你可以通过这些获得某些地方,但可能只需要限制事件。

这就是我的尝试:

using System.Linq;
using WinRTXamlToolkit.Controls.Extensions;
using WinRTXamlToolkit.Debugging;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Shapes;

namespace App91
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            this.Loaded += OnLoaded;
        }

        private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
        {
            //WinRTXamlToolkit.Controls.Extensions.VisualTreeHelperExtensions.GetFirstDescendantOfType<>()
            var horizontalTemplateGrid =
                s1.GetDescendantsOfType<Grid>()
                .First(g => g.Name == "HorizontalTemplate");

            horizontalTemplateGrid.PointerPressed += OnPointerPressed;
            horizontalTemplateGrid.PointerReleased += OnPointerReleased;
            horizontalTemplateGrid.PointerMoved += OnPointerMoved;

            var rect =
                s1.GetDescendantsOfType<Rectangle>()
                .First(g => g.Name == "HorizontalTrackRect");

            rect.PointerPressed += OnPointerPressed;
            rect.PointerReleased += OnPointerReleased;
            rect.PointerMoved += OnPointerMoved;

            rect =
                s1.GetDescendantsOfType<Rectangle>()
                .First(g => g.Name == "HorizontalDecreaseRect");

            rect.PointerPressed += OnPointerPressed;
            rect.PointerReleased += OnPointerReleased;
            rect.PointerMoved += OnPointerMoved;

            var thumb = s1.GetFirstDescendantOfType<Thumb>();
            thumb.DragStarted += ThumbOnDragStarted;
            thumb.DragCompleted += ThumbOnDragCompleted;
            thumb.PointerPressed += OnPointerPressed;
            thumb.PointerReleased += OnPointerReleased;
            this.PointerPressed += OnPointerPressed;
            this.PointerReleased += OnPointerReleased;
        }

        private void OnPointerMoved(object sender, PointerRoutedEventArgs pointerRoutedEventArgs)
        {
            if (pointerRoutedEventArgs.Pointer.IsInContact)
            {
                DC.Trace("Pointer Moved");
            }
        }

        private void OnPointerReleased(object sender, PointerRoutedEventArgs pointerRoutedEventArgs)
        {
            DC.Trace(
                "Pointer Released on " +
                pointerRoutedEventArgs.OriginalSource.GetType() +
                " named " +
                ((FrameworkElement)pointerRoutedEventArgs.OriginalSource).Name);
        }

        private void OnPointerPressed(object sender, PointerRoutedEventArgs pointerRoutedEventArgs)
        {
            DC.Trace(
                "Pointer Pressed on " +
                pointerRoutedEventArgs.OriginalSource.GetType() +
                " named " +
                ((FrameworkElement)pointerRoutedEventArgs.OriginalSource).Name);
        }

        private void ThumbOnDragCompleted(object sender, DragCompletedEventArgs dragCompletedEventArgs)
        {
            DC.Trace("Drag Completed");
        }

        private void ThumbOnDragStarted(object sender, DragStartedEventArgs dragStartedEventArgs)
        {
            DC.Trace("Drag Started");
        }

        private void S1_OnValueChanged(object sender, RangeBaseValueChangedEventArgs e)
        {
            DC.Trace("Value Changed");
        }
    }
}