Switching Between System Power Saving Mode in UWP app ,makes the app non-responded for a while

Prem Kumar S 0 Reputation points
2024-06-28T10:13:12.27+00:00

My UWP app was not responding for sometime, when switching between power Saving mode(windows settings) while App was running. To Reproduce the issue ,

  1. Run the app
  2. Open Windows System Battery Settings -> Enable/Disable Power Saving Mode
  3. 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

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
11,033 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,562 questions
Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,550 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
780 questions
{count} votes