【问题标题】:ToggleSwitch in UWP C#UWP C# 中的切换开关
【发布时间】:2018-04-06 23:03:47
【问题描述】:

我有一个小问题。我正在尝试创建一个 UWP C# 应用程序,并且我在项目中使用切换开关。如果我经常从软件切换 ToggleSwitch 的状态,内存使用量会增加很多。为什么会这样?

ToggleSwitch 表示使用绑定的布尔值。

我已经创建了示例代码:

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>

C#:

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));
        }
    }
}

我做了一些不可能的事情,我理解错了什么?

当我将 ToggleSwitch 替换为 CheckBox 时,内存使用量增加得更少。

【问题讨论】:

    标签: c# xaml uwp


    【解决方案1】:

    您的应用程序似乎发生了内存泄漏,这解释了 GC 未能释放它的原因。

    一些注意事项:

    • 为什么要在页面级别实现INotifyPropertyChanged?你应该在一个单独的类上实现它。该类应该保存模型并实现INotifyPropertyChanged 接口本身。 该类通常命名为ViewModel,顾名思义,它包含视图的模型,它知道如何将任何更改传达给它。

      • 通常大多数与 Binding 相关的内存泄漏问题(已报告)通常发生在使用编译绑定 {x:Bind ...} 时,但您使用的是传统绑定。您已经正确地执行了对依赖属性的绑定,例如 IsOn 并且还在源对象上实现了 INotifyPropertyChanged,所以我认为您在启动绑定过程的方式上并不存在固有的错误。

      • 您正在连续执行 100 次绑定更新,因此即使 CPU 使用率应该稍低一些,但预计在您迭代循环时,您的 CPU 使用率会增加。 Ps:由于您没有将方法标记为“异步”,因此您将阻塞 UI 线程,直到您的循环操作结束,这可能是您可能想要更改的内容。 绑定更新当然是一个过程,其中每个操作都有几个动作,这就是为什么我“有信心”CPU 使用率可能不是大问题的原因。

    我实际上没有猜测,但我强烈建议重构您的代码以将 ViewModel 类实现为一个额外的类,并观察观察到的内存泄漏是否存在。

    【讨论】:

      【解决方案2】:

      看起来您的调用正在耗尽 CPU。

      我的回答是你应该重新考虑你的应用架构。

      【讨论】:

        猜你喜欢
        • 2020-07-28
        • 2020-12-01
        • 2013-01-24
        • 2021-02-02
        • 1970-01-01
        • 2017-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多