0

我有以下代码用于控制 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 完全可以做到这一点。

4

1 回答 1

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");
        }
    }
}
于 2013-01-15T07:13:48.140 回答