Switching Between System Power Saving Mode in UWP app ,makes the app non-responded for a while
My UWP app was not responding for sometime, when switching between power Saving mode(windows settings) while App was running. To Reproduce the issue ,
- Run the app
- Open Windows System Battery Settings -> Enable/Disable Power Saving Mode
- Now try to bring our UWP app window to Foreground(front) -> It takes sometime to bring the app to foreground state from background state.
My XAML Code :
<Canvas x:Name="MyCanvas" Background="Aqua"/>
My C# Code :
RelativePanel buttonsPanel = new RelativePanel();
Button previousButton = null;
for (int i = 0; i < 500; i++)
{
Button currentButton = new Button();
currentButton.Width = 10;
currentButton.Height = 10;
currentButton.Background = new SolidColorBrush(Colors.Blue);
currentButton.BorderBrush = new SolidColorBrush(Colors.Red);
buttonsPanel.Children.Add(currentButton);
if (previousButton != null)
{
RelativePanel.SetBelow(currentButton, previousButton);
}
previousButton = currentButton;
}
MyCanvas.Children.Add(buttonsPanel);
I just add 500 buttons inside relative panel and aligned the each button below the above one using RelativePanel.SetBelow(currentButton, previousButton).
If I remove the RelativePanel.SetBelow() line, the app respond properly as expected. But I want to align the children inside RelativePanel relatively. On the other hand, If I move the above c# code completely to XAML, it works fine in that case also. Example ...
XAML :
<Canvas x:Name="MyCanvas" Background="Aqua">
<RelativePanel>
<Border Name="b0" Width="10" Height="10" Background="Blue" BorderBrush="Red" />
<Button Name="b1" Width="10" Height="10" Background="Blue" BorderBrush="Red" RelativePanel.Below="b0" />
<Button Name="b2" Width="10" Height="10" Background="Blue" BorderBrush="Red" RelativePanel.Below="b1" />
<Button Name="b3" Width="10" Height="10" Background="Blue" BorderBrush="Red" RelativePanel.Below="b2" />
<Button Name="b4" Width="10" Height="10" Background="Blue" BorderBrush="Red" RelativePanel.Below="b3" />
<Button Name="b5" Width="10" Height="10" Background="Blue" BorderBrush="Red" RelativePanel.Below="b4" />
.
.
.
.
<Button Name="b500" Width="10" Height="10" Background="Blue" BorderBrush="Red" RelativePanel.Below="b499" />
</RelativePanel>
</Canvas>
Atleast we showld have 500 Buttons inside the relative panel to reproduce the issue. I don't know why this is happening, when i write the code completely in XAML , it works fine only. But if I add the buttons in C# and aligned relatively, the app won't respond properly while switching between power saving mode. I think the issue is with setting the Relativepanel.SetBelow() line. My Sample Project link https://drive.google.com/drive/folders/1tn8-m_SJYsOvZ8atEGASmXrrP-z2QfU8?usp=sharing