ToggleSwitch in UWP C #

I have a little question. I am trying to create a UWP C # application and I am using toggle switches in a project. If I switch ToggleSwitch status from software very often, memory usage is greatly increased. Why is this happening?

ToggleSwitch is a boolean using binding.

I created an example code:

XAML:

<Page x:Class="App1.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App1" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <ToggleSwitch Header="Test" HorizontalAlignment="Left" Margin="213,27,0,0" VerticalAlignment="Top" IsOn="{Binding Path=TestBool, Mode=TwoWay}"/> <CheckBox Content="Two-state CheckBox" Margin="108,162,0,806" IsChecked="{Binding Path=TestBool, Mode=TwoWay}"/> <Button Content="Start!" HorizontalAlignment="Left" Margin="69,58,0,0" VerticalAlignment="Top" Click="Button_Click"/> </Grid> 

WITH#:

 using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using System.ComponentModel; using System.Runtime.CompilerServices; // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 namespace App1 { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> /// public sealed partial class MainPage : Page, INotifyPropertyChanged { private bool testBool = false; public bool TestBool { get { return testBool; } set { if (testBool != value) { testBool = value; OnPropertyChanged(); } } } public MainPage() { DataContext = this; this.InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { for (int i = 0; i < 10000; i++) { TestBool = !TestBool; } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } } 

Am I doing something that is impossible, that I misunderstand?

When I replace ToggleSwitch with CheckBox, the memory usage is much less.

Memory usage

+5
source share
2 answers

It looks like your call is shutting down the processor.

enter image description here

My answer: you should think about your application architecture again.

0
source

It seems that a memory leak is occurring in your application, explaining why the GC was unable to free it.

Some considerations:

  • Why are you implementing INotifyPropertyChanged -level INotifyPropertyChanged ? You must implement it in a separate class. This class must contain a model and implement the INotifyPropertyChanged interface. This class is usually referred to as a ViewModel , and as the name implies, it contains a model for the view, and it knows how to pass any changes to it.

    • Typically, most memory leak problems with Bindings are reported to typically occur when using compiled bindings, {x: Bind ...}, but you are using traditional bindings. You correctly bound to a dependency property, such as IsOn , and also implemented INotifyPropertyChanged in the source object, so I don’t think that there is something inherently wrong with how you rotate the binding process.

    • You are doing 100 binding updates per line, so even if the CPU usage should be slightly lower, it is expected that you will increase your CPU usage during the loop repeat. Ps: Since you did not mark your method as "asynchronous", you will block the user interface thread until the loop operation finishes, which may be what you might want to change. Updating a binding, of course, is a process with several actions in the middle for each, so I’m sure that using the CPU cannot be a big problem.

Actually, I have no assumption, but I would strongly recommend reorganizing your code so that the ViewModel class is implemented as an additional class and observes whether memory leak detection is observed.

0
source

Source: https://habr.com/ru/post/1276282/


All Articles