I have a requirement on WPF to play a video repeatedly on idle event and restore back to the previous window on the detection of user activity.
For this, I followed the answered https://stackoverflow.com/a/4970019/6696609 by Martin Buberl.
In XAML, I have VideoGrid and WindowGrid Grid, With the detection of idle event, I changed the visibility of each other.
Here is the source code for the illustration of described issue https://github.com/DavidSilwal/wpfvideoissue The idle event is suppose to be occurred at 5 sec. When you click on button, idle event occurred at the 10 sec.
It works perfectly fine except
For the first idle event of the button click, VideoGrid attempts to be visible but it couldn’t be visible (it just blinks) and then every next idle event worked fine.
Feedback and suggestion to get rid of the blink issue are appreciated.
// 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();
}
}