gpt4 book ai didi

silverlight - 为什么不对Silverlight ContentControls进行垃圾收集?

转载 作者:行者123 更新时间:2023-12-04 16:14:28 26 4
gpt4 key购买 nike

我一直在研究为什么不对我的某些控件进行垃圾回收,并且注意到很容易防止从ContentControl继承的简单控件被破坏。这是一个例子:

这是我的自定义ContentControl:

 public class MyCustomControl : ContentControl
{

public MyCustomControl()
{
Debug.WriteLine("Constructed");
}

~MyCustomControl()
{
Debug.WriteLine("Destroyed");
}
}

现在,如果我将其放在这样的页面上:
<navigation:Page x:Class="SimpleTestBed.Views.CustomControl" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
xmlns:local="clr-namespace:SimpleTestBed"
d:DesignWidth="640" d:DesignHeight="480"
Title="CustomControl Page">
<Grid x:Name="LayoutRoot">

<StackPanel>
<local:MyCustomControl>
<TextBox Text="{Binding SomeProperty,Mode=TwoWay}"></TextBox>
</local:MyCustomControl>
</StackPanel>

</Grid>

后面有以下代码:
 public partial class CustomControl : Page
{
public CustomControl()
{
InitializeComponent();

this.DataContext = new CustomControlViewModel();

this.Unloaded += new RoutedEventHandler(OnUnloaded);
}

void OnUnloaded(object sender, RoutedEventArgs e)
{
this.DataContext = null;
}

// Executes when the user navigates to this page.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}



}

然后, View 模型为:
  public class CustomControlViewModel : INotifyPropertyChanged
{

#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
RaisePropertyChanged(propertyName);
}
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion


private string _someProperty = "Initial Value";
public string SomeProperty
{
get { return _someProperty; }
set
{
if (_someProperty != value)
{
string oldValue = _someProperty;
_someProperty = value;
OnPropertyChanged("SomeProperty");
OnSomePropertyChanged(oldValue, value);
}
}
}



protected virtual void OnSomePropertyChanged(string oldValue, string newValue)
{

}


}

现在,当我离开此页面并尝试使用GC.Collect()进行垃圾收集时,只要我未对Textbox中的文本进行任何更改,ContentControl和Page就会被GC破坏。但是,如果我编辑了一些文本并离开了页面,然后尝试使用GC.Collect(),则ContentControl不会收集垃圾。

谁能解释这种行为?

实际上,您可以在卸载时通过“闪烁”控件的模板来强制GC收集控件:
  void MyCustomControl_Unloaded(object sender, RoutedEventArgs e)
{
Debug.WriteLine("MyCustomControl Unloaded");
ControlTemplate oldTemplate = this.Template;
this.Template = null;
this.Template = oldTemplate;
}

我猜想这会破坏当前的可视树,丢失树的第一个组件对其父对象(自定义控件)的引用。当重新加载控件时,它肯定会强制控件重新调用OnApplyTemplate。

这是开发Silverlight控件而不泄漏的正确模式吗?如果是这样,让我感到有些奇怪的是,当控件卸载时,模板不会自动处理。

很好地说明此行为,因为它正好关系到Silverlight控件生命周期的核心。

最佳答案

我的经验表明,Silverlight中的内存泄漏是由前2个原因引起的:

  • Events =>确保在不需要或在类析构函数中删除附加事件一次。
  • Templates =>解决方案在“资源”部分
  • 中定义模板

    关于silverlight - 为什么不对Silverlight ContentControls进行垃圾收集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4287958/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com