为什么Silverlight ContentControls不是垃圾回收?

Ste*_*lis 6 silverlight lifecycle user-controls

我一直在调查为什么我的一些控件没有被垃圾收集,并注意到很容易阻止从ContentControl继承的简单控件被破坏.这是一个例子:

这是我的自定义ContentControl:

 public class MyCustomControl : ContentControl
{

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

    ~MyCustomControl()
    {
        Debug.WriteLine("Destroyed");
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我把它放在这样的页面上:

<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>
Run Code Online (Sandbox Code Playgroud)

使用以下代码:

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



}
Run Code Online (Sandbox Code Playgroud)

那么视图模型是:

  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)
    {

    }


}
Run Code Online (Sandbox Code Playgroud)

现在,当我离开此页面并尝试使用GC.Collect()进行垃圾收集时,只要我没有对文本框中的文本进行任何更改,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;
    }
Run Code Online (Sandbox Code Playgroud)

我认为这会破坏当前的可视树,将树的第一个组件的引用丢失给它的父(自定义控件).它肯定会强制控件在重新加载控件时调用OnApplyTemplate.

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

很好地理解这种行为,因为它正好适用于Silverlight控件生命周期的核心.

Den*_*sic -1

我的经验表明 Silverlight 中的内存泄漏是由以下两个原因引起的:

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