WPF DataContent内存释放问题

来源:互联网 发布:辣木籽退伍军人淘宝 编辑:程序博客网 时间:2024/06/07 05:31


最近发现一个有趣的事,通过Window.dataContent来设置绑定以后,

窗体关闭以后 ViewModel 并没有释放,再次调用改窗体后,前一个ViewModel才释放,

或者是程序关闭以后才释放 ,搞不懂。。。

如果要马上释放ViewModel可以在Window关闭以后将DataContent 设置为null ,

这样ViewModel是可以释放的

下面是测试代码,大神请指教!


主窗体:

<Window x:Class="WeakViewModel.MainWindow"
        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"
        xmlns:local="clr-namespace:WeakViewModel"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Content="Win1"  HorizontalAlignment="Left" Margin="367,211,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
        <Button Content="GC" HorizontalAlignment="Left" Margin="128,211,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
    </Grid>
</Window>

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            TestWindow win = new TestWindow();
            win.Owner = this;
            win.DataContext = ViewModel.Default;
            win.Show();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {            
            GC.Collect();
        }
    }


测试窗体:

<Window x:Class="WeakViewModel.TestWindow"
        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"
        xmlns:local="clr-namespace:WeakViewModel"
        mc:Ignorable="d"       
        Title="TestWindow" Height="300" Width="300">
    <Grid>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="96,111,0,0" TextWrapping="Wrap" Text="{Binding Path=Text}" VerticalAlignment="Top" Width="120"/>
    </Grid>
</Window>

    public partial class TestWindow : Window
    {
        public TestWindow()
        {
            InitializeComponent();
            Closed += TestWindow_Closed;
        }
        ~ TestWindow()
        {
            MessageBox.Show("释放TestWindow");
        }
        private void TestWindow_Closed(object sender, EventArgs e)
        {

            //注释这个行就能看出差别

            DataContext = null;
        }
    }


测试ViewModel:

    public class ViewModel
    {
        private static WeakReference _default = null;
        private static object locker = new object();
        public static ViewModel Default
        {
            get
            {
                if(_default==null||!_default.IsAlive)
                {
                    lock (locker)
                    {
                        _default = new WeakReference(new ViewModel());
                    }
                }
                return _default.Target as ViewModel;
            }
        }

        private ViewModel()
        {

        }

         ~ViewModel()
        {
            MessageBox.Show("释放 ViewModel"+Text);
        }

        private string _text = string.Empty;
        public string Text
        {
            get
            {
                return _text;
            }
            set
            {
                _text = value;
            }
        }

    }



原创粉丝点击