Idle Window Page WPF - c#

I'm detecting inactivity of my WPF project. I've been using this code.
readonly DispatcherTimer activityTimer;
public MainWindow()
{
InitializeComponent();
InputManager.Current.PreProcessInput += Activity;
activityTimer = new DispatcherTimer
{
Interval = TimeSpan.FromSeconds(5),
IsEnabled = true
};
activityTimer.Tick += Inactivity;
}
#region
void Inactivity(object sender, EventArgs e)
{
FrameBody.Source = new Uri("Pages/Body.xaml", UriKind.Relative);
}
void Activity(object sender, PreProcessInputEventArgs e)
{
activityTimer.Stop();
activityTimer.Start();
}
#endregion
It do work but there is only one problem. When the user doesn't do anything with the system, it do change the source of the frame FrameBody.Source = new Uri("Pages/Body.xaml", UriKind.Relative); But it also goes to the recent source of the frame.
In my system,I'm using 1 window with Frame. And the source of that frame changes depending on the user's interaction with the system. (ex. When the user click button then the source of that frame changes and when no click or sudden movement of the mouse for several seconds/minute, the source of the frame will change.)
Can anyone tell me what's wrong with my code? The code above is based from this WPF inactivity and activity

Related

Optimizing Media Player and Slider in WPF

I am making a WPF application that is similar to a video player. There is a MediaElement for the video, and a slider that is bound to the video. I also want the current timestamp to appear.
public MainWindow()
{
InitializeComponent();
DataContext = VideoViewModel;
ViewModel1.video_timestamp = "00:00:00:00";
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(1);
timer.Tick += timer_Tick;
timer.Start();
}
private void timer_Tick(object sender, EventArgs e) {
if ((VideoElement.Source != null) && (VideoElement.NaturalDuration.HasTimeSpan))
{
Slider1.Minimum = 0;
Slider1.Maximum = VideoElement.NaturalDuration.TimeSpan.TotalMilliseconds;
Slider1.Value = VideoElement.Position.TotalMilliseconds;
}
}
private void VidSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
ViewModel1.video_timestamp = TimeSpan.FromMilliseconds(Slider1.Value).ToString(#"hh\:mm\:ss\:ff");
VideoElement.Position = TimeSpan.FromMilliseconds(Slider1.Value);
DataContext = ViewModel1;
}
VidSlider_ValueChanged should update the textbox that shows the current video timestamp. So when a video is playing, this function is called every tick. When the user drags the thumb, this function is also called. The problem is that this seems to slow the application down especially when the video is long and when the user keeps on dragging the thumb quickly.
I am thinking about doing some Threading but I'm at a loss with the syntax since this some kind of click event(?) in XAML. Can I have some suggestions?
PS. I should display in milliseconds because this data will be used for another application later on.

WPF- how to create a kind of a screensaver (with timer)

I do not know how to solve this problem:
I want in my WPF Application something like a screensaver which pops up (let's say after 20 seconds) if you do not interact with the program. I tried researching, but I did not find anything and I do not know how to start. Does anyone have any tips for me?
You can for example start DispacherTimer and reset it if person move mouse or click something. In event check is time's up start new window/popup/dialog modal with screensaver, if person move mouse or click in screensaver close it and back to previous window.
Something about DispacherTimer you can find here:
WPF Timer Like C# Timer
or here https://msdn.microsoft.com/en-gb/library/system.windows.threading.dispatchertimer%28v=vs.90%29.aspx
You can do something like this then all you have to do is just set a timmer in the app.cs that resets.
private DispatcherTimer _timer;
protected override void OnStartup(StartupEventArgs e)
{
_timer = new DispatcherTimer();
_timer.Tick += Timer_Tick;
_timer.Interval = new TimeSpan(0,0,0,20,0);
_timer.Start();
EventManager.RegisterClassHandler(typeof(Window),Window.MouseMoveEvent, new RoutedEventHandler(Reset_Timer));
EventManager.RegisterClassHandler(typeof(Window), Window.MouseDownEvent, new RoutedEventHandler(Reset_Timer));
EventManager.RegisterClassHandler(typeof(Window), Window.KeyDownEvent, new RoutedEventHandler(Reset_Timer));
}
private void Timer_Tick(object sender, EventArgs e)
{
MessageBox.Show("Ticked");
}
private void Reset_Timer(object sender, EventArgs e)
{
_timer.Interval = new TimeSpan(0,0,0,20,0);
}
This will reset the timer every time you move your mouse click your mouse or push any key in the window.

WinForm button background image updates only every other run

I want a blinking LED -- alternately a lighted, then a dark image in a PictureBox -- to appear during a run (that I click a button to start). The image lights when the run starts, and goes dark when it the run finishes. That always works.
This code:
this.timer.SynchronizingObject = this;
this.timer.Interval = 250;
this.timer.Elapsed += (s, ea) =>
{
this.ledLit = !this.ledLit;
ShowInLog(this.ledLit ? "/" : "\\");
this.picMarking.BackgroundImage = this.ledLit ? this.imageStopped : this.imageRunning;
this.picMarking.Refresh();
};
works great to show a blinking LED image during the run...every other run.
On every even-numbered invocation, the display of alternating slashes shows that the timer is working, but the background image does not update (except perhaps a rare flicker).
Why? How do I make it to work on every invocation?
Here's some quick code I put together for your "blinking" effect:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Blinker
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 250;
timer1.Tick += timer1_Tick;
timer1.Start();
}
void timer1_Tick(object sender, EventArgs e)
{
Console.WriteLine("Tick");
pictureBox1.BackColor = (pictureBox1.BackColor == System.Drawing.Color.Red) ? System.Drawing.Color.Black : System.Drawing.Color.Red;
}
}
}
I've used event handlers for the Tick event on the timer to trigger the blinking. You can adjust the image like I have with the BackColor property. It's a quick and dirty approach, but it gets the blinking effect achieved.
I found the answer to my problem here: Custom event handler is repeating itself...
My problem is that I subscribed to the tick event each time, rather than only once.

Navigate to Button Click Event in Lens Application for Windows Phone 8

Upon pressing the Lens tile icon for my application within the default camera application on Windows Phone, I would like to navigate to my application. I have this working, although I am using the CameraCaptureTask to take photos and then I save and display them within my MainPage. I am calling CameraCaptureTask on a click event, so my question is how might I access this click event when I select the Application Tile in the Lens picker?
App.xaml.cs
private void InitializePhoneApplication()
{
if (phoneApplicationInitialized)
return;
// Create the frame but don't set it as RootVisual yet; this allows the splash
// screen to remain active until the application is ready to render.
//RootFrame = new PhoneApplicationFrame();
RootFrame = new TransitionFrame();
RootFrame.Navigated += CompleteInitializePhoneApplication;
// Assign the lens example URI-mapper class to the application frame.
RootFrame.UriMapper = new LensUriMapper();
// Handle navigation failures
RootFrame.NavigationFailed += RootFrame_NavigationFailed;
// Handle reset requests for clearing the backstack
RootFrame.Navigated += CheckForResetNavigation;
// Ensure we don't initialize again
phoneApplicationInitialized = true;
}
LensUriMapper.cs
class LensUriMapper : UriMapperBase
{
private string tempUri;
public override Uri MapUri(Uri uri)
{
tempUri = uri.ToString();
// Look for a URI from the lens picker.
if (tempUri.Contains("ViewfinderLaunch"))
{
// Launch as a lens, launch viewfinder screen.
//return new Uri("/Views/MainPage.xaml", UriKind.Relative);
return new Uri("/Views/MainPage.xaml?Viewfinder=1", UriKind.Relative);
}
}
// Otherwise perform normal launch.
return uri;
}
}
MainPage.xaml.cs
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (NavigationContext.QueryString.ContainsKey("Viewfinder"))
{
newButton_Click(null, null);
}
}
void newButton_Click(object sender, EventArgs e)
{
_cameraTask.Show();
}
In addition, how might I navigate directly back to the default camera application upon the back key press as stated in MSDN's Lens application guidelines?
you can add a parameter to the ViewfinderLaunch uri:
return new Uri("/Views/MainPage.xaml?Viewfinder=1", UriKind.Relative);
Next you have to override the "OnNavigatedTo" method of your MainPage to check if Viewfinder parameter is setted:
if (NavigationContext.QueryString.ContainsKey("Viewfinder"))
{
newButton_Click(null, null);
}

Raise event after 30 seconds of no user interaction

I'm programming a WPF application, and I'd like to raise an event if the user has not interacted with the program for 30 seconds. That is to say, no keyboard or/and mouse events.
The reason I want do do this is because I want to bring attention to the screen if a variable alertstate has been set to true.
I'm thinking of using something along the lines of BackgroundWorker but I really don't know how I can get the time a user has not interacted with the program. Can someone point me in the right direction?
I guess this question basically comes down to checking if a user has interacted with the screen. How do I do this?
One way you could do this is to use GetLastInputInfo. This information will give you the time elapsed (in ticks) since last user interaction on mouse/keyboard.
You can have information here :
http://www.pinvoke.net/default.aspx/user32.GetLastInputInfo
So have a timer which checks for the last time an interaction went on. If you need accuracy, you can either check each 5 second for example OR you can, when you see that idle is ongoing for y seconds (y<30), setup a one-time timer that will check for idle time after (30-y) seconds.
You need to record the last time the user moved the mouse or pressed a key and then check if that time is greater than your threshold.
So you need to add mouse move, mouse click and keyboard handlers to your application (this is Silverlight code so you might have to change namespaces etc.):
private void AttachEvents()
{
Application.Current.RootVisual.MouseMove += new MouseEventHandler(RootVisual_MouseMove);
Application.Current.RootVisual.KeyDown += new KeyEventHandler(RootVisual_KeyDown);
Application.Current.RootVisual.AddHandler(UIElement.MouseLeftButtonDownEvent, (MouseButtonEventHandler)RootVisual_MouseButtonDown, true);
Application.Current.RootVisual.AddHandler(UIElement.MouseRightButtonDownEvent, (MouseButtonEventHandler)RootVisual_MouseButtonDown, true);
}
Then in the handlers have code like this for the mouse move:
private void RootVisual_MouseMove(object sender, MouseEventArgs e)
{
timeOfLastActivity = DateTime.Now;
}
and a similar one for the KeyDown event handler.
You will have to set off a timer:
idleTimer = new DispatcherTimer();
idleTimer.Interval = TimeSpan.FromSeconds(1);
idleTimer.Tick += new EventHandler(idleTimer_Tick);
// Initialise last activity time
timeOfLastActivity = DateTime.Now;
Then in the tick event handler have something like this:
private void idleTimer_Tick(object sender, EventArgs e)
{
if (DateTime.Now > timeOfLastActivity.AddSeconds(30))
{
// Do your stuff
}
}
Use ComponentDispatcher.ThreadIdle and DispatcherTimer to achieve this.
DispatcherTimer timer;
public Window1()
{
InitializeComponent();
ComponentDispatcher.ThreadIdle += new EventHandler(ComponentDispatcher_ThreadIdle);
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(30);
timer.Tick += new EventHandler(timer_Tick);
}
void timer_Tick(object sender, EventArgs e)
{
//Do your action here
timer.Stop();
}
void ComponentDispatcher_ThreadIdle(object sender, EventArgs e)
{
timer.Start();
}

Categories