视频在空闲事件上反复出现,并在检测到用户活动时恢复到上一个​​窗口

时间:2019-05-22 07:38:15

标签: c# wpf winforms

我要求WPF在发生空闲事件时重复播放视频,并在检测到用户活动时恢复到上一个​​窗口。

为此,我跟随马丁·布伯尔(Martin Buberl)回答的https://stackoverflow.com/a/4970019/6696609

在XAML中,我具有VideoGrid和WindowGrid Grid,通过检测空闲事件,我改变了彼此的可见性。

以下是说明问题https://github.com/DavidSilwal/wpfvideoissue的说明的源代码 假定空闲事件发生在5秒。当您单击按钮时,空闲事件在10秒时发生。

除了

之外,它都可以正常工作

对于按钮单击的第一个空闲事件,VideoGrid尝试可见,但不可见(它只是闪烁),然后每个下一个空闲事件都可以正常工作。

感谢您提供反馈和建议以消除眨眼问题。

    // set UI on inactivity
    private  void OnInactivity(object sender, EventArgs e)
    {
        _inactiveMousePosition = Mouse.GetPosition(this);                    

        System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =>
          {
              if (WindowGrid.Visibility == Visibility.Visible)
              {
                  WindowGrid.Visibility = Visibility.Collapsed;
              }

              if (VideoGrid.Visibility == Visibility.Collapsed)
              {
                  WindowState = WindowState.Maximized;
                  WindowStyle = WindowStyle.None;
                  //play video
                  videoplayer.Play();
                  VideoGrid.Visibility = Visibility.Visible;

              }
          }));
    }

     private void OnActivity(object sender, PreProcessInputEventArgs e)
    {
        var inputEventArgs = e.StagingItem.Input;

        if (inputEventArgs is System.Windows.Input.MouseEventArgs || inputEventArgs is KeyboardEventArgs)
        {
            if (e.StagingItem.Input is System.Windows.Input.MouseEventArgs)
            {
                var mouseEventArgs = (System.Windows.Input.MouseEventArgs)e.StagingItem.Input;

                 // no button is pressed and the position is still the same as the application became inactive       
                if (!(
                    mouseEventArgs.LeftButton == MouseButtonState.Pressed ||
                    mouseEventArgs.RightButton == MouseButtonState.Pressed ||
                    mouseEventArgs.MiddleButton == MouseButtonState.Pressed ||
                    mouseEventArgs.XButton1 == MouseButtonState.Pressed ||
                    mouseEventArgs.XButton2 == MouseButtonState.Pressed
                    //|| _inactiveMousePosition != mouseEventArgs.GetPosition(this)
                    ))
                {
                    return;
                }
            }

            // set UI on activity
            System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =>
              {
                  if (VideoGrid.Visibility == Visibility.Visible)
                  {
                      WindowState = WindowState.Normal;
                      WindowStyle = WindowStyle.SingleBorderWindow;
                      //stop video
                      videoplayer.Stop();
                      VideoGrid.Visibility = Visibility.Collapsed;
                  }

                  if (WindowGrid.Visibility == Visibility.Collapsed)
                  {
                      WindowGrid.Visibility = Visibility.Visible;
                  }
              }));

            _activityTimer.Stop();
            _activityTimer.Start();
        }
    }

1 个答案:

答案 0 :(得分:2)

通过调试代码,我可以给您一个答案,但可能不是一个令人满意的答案。

我在OnInactivity中为您的代码添加了一些调试输出...

private async void OnInactivity(object sender, EventArgs e)
{
    // remember mouse position
    _inactiveMousePosition = Mouse.GetPosition(this);
    // set UI on inactivity

    Debug.WriteLine ( DateTime.Now.ToString("HH:mm:ss.fffffff") + " Inactivity" ) ;

    await System.Windows.Application.Current.Dispatcher.InvokeAsync(new Action(() =>
    {
        Grid1.Visibility = Visibility.Collapsed;
        videoplayer.Play();
        Grid2.Visibility = Visibility.Visible;
    }));
}

以及在OnActivity中...

private async void OnActivity(object sender, PreProcessInputEventArgs e)
{
    var inputEventArgs = e.StagingItem.Input;

    if (inputEventArgs is System.Windows.Input.MouseEventArgs || inputEventArgs is KeyboardEventArgs)
    {
        if (e.StagingItem.Input is System.Windows.Input.MouseEventArgs)
        {
            var mouseEventArgs = (System.Windows.Input.MouseEventArgs)e.StagingItem.Input;

            if (!(
                mouseEventArgs.LeftButton == MouseButtonState.Pressed ||
                mouseEventArgs.RightButton == MouseButtonState.Pressed ||
                mouseEventArgs.MiddleButton == MouseButtonState.Pressed ||
                mouseEventArgs.XButton1 == MouseButtonState.Pressed ||
                mouseEventArgs.XButton2 == MouseButtonState.Pressed
                || _inactiveMousePosition != mouseEventArgs.GetPosition(this)
                ))
            {
                return;
            }
        }

        Debug.WriteLine ( DateTime.Now.ToString("HH:mm:ss.fffffff") + " Activity " + inputEventArgs.RoutedEvent.ToString() ) ;

        // set UI on activity
        await System.Windows.Application.Current.Dispatcher.InvokeAsync(new Action(() =>
        {
            videoplayer.Stop();
            Grid1.Visibility = Visibility.Visible;

            Grid2.Visibility = Visibility.Hidden;
        }));

        _activityTimer.Stop();
        _activityTimer.Start();
    }
}

按下按钮后,这里显示的是一点输出:

23:45:31.0583181 Activity Mouse.MouseMove
23:45:31.0643177 Activity Mouse.QueryCursor
23:45:31.0653178 Activity Mouse.PreviewMouseMove
23:45:31.0663188 Activity Mouse.MouseMove
23:45:31.0723180 Activity Mouse.QueryCursor
23:45:31.0753176 Activity Mouse.PreviewMouseMove
23:45:31.0763193 Activity Mouse.MouseMove
23:45:36.0722537 Inactivity
23:45:36.0752537 Activity Keyboard.PreviewGotKeyboardFocus
23:45:36.0782576 Activity Keyboard.PreviewKeyboardInputProviderAcquireFocus
23:45:36.0802536 Activity Keyboard.KeyboardInputProviderAcquireFocus
23:45:36.0832529 Activity Keyboard.LostKeyboardFocus
23:45:36.0842549 Activity Keyboard.GotKeyboardFocus
23:45:41.1041883 Inactivity
23:45:42.4981698 Activity Mouse.QueryCursor
23:45:42.5001740 Activity Mouse.PreviewMouseMove
23:45:42.5031736 Activity Mouse.MouseMove
23:45:42.5071741 Activity Mouse.QueryCursor
23:45:42.5091736 Activity Mouse.PreviewMouseMove
23:45:42.5111735 Activity Mouse.MouseMove

闲置5秒钟后,它会尝试开始播放视频,但立即会引起一些与焦点改变有关的事件。

我想这意味着单击按钮会使按钮成为焦点。开始播放视频会使视频失去焦点,从而产生一些事件。

我能够通过过滤掉这些事件来解决问题。这是我对OnActivity函数的修改版本,在其中我按名称过滤了这些事件:

private async void OnActivity(object sender, PreProcessInputEventArgs e)
{
    var inputEventArgs = e.StagingItem.Input;

    switch ( inputEventArgs.RoutedEvent.Name )
    {
        case "PreviewGotKeyboardFocus":
        case "PreviewKeyboardInputProviderAcquireFocus":
        case "KeyboardInputProviderAcquireFocus":
        case "LostKeyboardFocus":
        case "GotKeyboardFocus":
            return ;
    }

    if (inputEventArgs is System.Windows.Input.MouseEventArgs || inputEventArgs is KeyboardEventArgs)
    {
        if (e.StagingItem.Input is System.Windows.Input.MouseEventArgs)
        {
            var mouseEventArgs = (System.Windows.Input.MouseEventArgs)e.StagingItem.Input;

            if (!(
                mouseEventArgs.LeftButton == MouseButtonState.Pressed ||
                mouseEventArgs.RightButton == MouseButtonState.Pressed ||
                mouseEventArgs.MiddleButton == MouseButtonState.Pressed ||
                mouseEventArgs.XButton1 == MouseButtonState.Pressed ||
                mouseEventArgs.XButton2 == MouseButtonState.Pressed
                || _inactiveMousePosition != mouseEventArgs.GetPosition(this)
                ))
            {
                return;
            }
        }

        // set UI on activity
        await System.Windows.Application.Current.Dispatcher.InvokeAsync(new Action(() =>
        {
            videoplayer.Stop();
            Grid1.Visibility = Visibility.Visible;

            Grid2.Visibility = Visibility.Hidden;
        }));

        _activityTimer.Stop();
        _activityTimer.Start();
    }
}

我不知道是否应该过滤掉其他事件,也不知道按事件名称过滤是否是最好的方法。