Today we found a little strange problem in one of our WPF application and we are not exactly sure how to deal with it. When we run the software on one of our laptops the screen goes to sleep (turns black) after a while when there is no user interaction. It application itself is still busy downloading data from the internet and filling tables in a database. There is also a progress bar and a status bar.
If we wake the screen simply by moving the mouse the program is shown again but some parts of the window are not refreshed. The progress bar is still showing values even if the process is completed and the status bar is also wrong.
From this time on you can work with the program like nothing happened but progress bar and status bar will only be redrawn if you minimize the complete window to the task bar and maximize it again. Or if you start another action that will also use progress bar or status bar.
This strange behavior caused a lot of confusion because the displayed data seems to be wrong after the screen awakes from sleep and you need to minimize and maximize the window to see what is really going on.
What is wrong here?
You need to handle the event
Microsoft.Win32.SystemEvents.PowerModeChanged
, see
http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents(v=VS.100).aspx
[
^
].
Look at this code sample:
http://msdn.microsoft.com/en-us/library/hxkc1kwd.aspx
[
^
].
For refreshing of the WPF Window try to call
System.Windows.Window.UpdateLayout
, see
http://msdn.microsoft.com/en-us/library/system.windows.window.aspx
[
^
].
Thank you for the interesting question again.
Good luck,
For whom is using the MaterialDesign libraries, it happens not only after going to sleep-mode but even after a simple screen-lock (Windows-L shortcut).
I solved adding this line to the Window element of the xaml file:
materialDesign:ShadowAssist.CacheMode=
"
{x:Null}"
Hope it helps.
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad
spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or
edit the question
and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it.
Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
///
/// Interaction logic for Window1.xaml
///
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.Loaded += (o, e) => { this.TabControlMain.SelectedIndex = 0; };
SystemEvents.PowerModeChanged+=new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged);
}
private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
if (e.Mode == PowerModes.Suspend)
{
PrepareToSleep();
}
else
{
this.UpdateLayout();
}
}
private void PrepareToSleep()
{
this.TabControlMain.SelectedIndex = 1;
}
}
TabControlMain is a tabcontrol ,since the window loaded ,the selectedindex is 0,
when the computer went to sleep ,its selectedindex turned to 1,but after awaken, the UI didn't update ——after i minimized it and clicked it in the taskbar, then it appeared with selecting the tabcontrol's 1 index.
in other words, it seems that UpdateLayout doesn't work as I expected.
Thanks for your help.