C# 处理通过ContentPresenter加载的UserControl
我的主视图(窗口)上有以下代码 这是我的另一个代码:C# 处理通过ContentPresenter加载的UserControl,c#,wpf,xaml,user-controls,C#,Wpf,Xaml,User Controls,我的主视图(窗口)上有以下代码 这是我的另一个代码: public class MyListBox : ListBox { public MyListBox() { Debug.WriteLine("Created instance of MyListBox:" + this.GetHashCode()); } ~MyListBox() { Debug.WriteLine("destroyed instance of MyListBox:" + this.GetHashCode(
public class MyListBox : ListBox
{
public MyListBox() { Debug.WriteLine("Created instance of MyListBox:" + this.GetHashCode()); }
~MyListBox() { Debug.WriteLine("destroyed instance of MyListBox:" + this.GetHashCode()); }
}
public class ViewModelBase : INotifyPropertyChanged
{
private PropertyChangedEventHandler _propertyChanged;
public event PropertyChangedEventHandler PropertyChanged
{
add
{
_propertyChanged += value;
}
remove
{
_propertyChanged -= value;
}
}
protected virtual void OnPropertyChanged(string property)
{
if (_propertyChanged != null)
{
_propertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
public class MainViewModel : ViewModelBase
{
private ChildViewModel _ChildViewModel;
public ChildViewModel ChildViewModel
{
get { return _ChildViewModel; }
set
{
if (_ChildViewModel != value)
{
_ChildViewModel = value;
OnPropertyChanged(nameof(ChildViewModel));
}
}
}
}
public class ChildViewModel : ViewModelBase
{
public ChildViewModel()
{
Debug.WriteLine("Created instance of ChildViewModel:" + this.GetHashCode());
this.Items = new List<string>(new string[] { "ABC", "DEF" });
}
~ChildViewModel()
{
this.Items = null;
Debug.WriteLine("destroyed instance of ChildViewModel:" + this.GetHashCode());
}
private IEnumerable<string> _Items;
public IEnumerable<string> Items
{
get { return _Items; }
set
{
if (_Items != value)
{
_Items = value;
OnPropertyChanged(nameof(Items));
}
}
}
}
当我将ChildView的XAML更改为:
<StackPanel>
<local:MyListBox>
</local:MyListBox>
</StackPanel>
所以,似乎我真的无法摆脱WPF的NotifyPropertyChangedEventHandler,所以我的列表框和视图将无法处理。我怎样才能做到这一点?因为我认为它正在清除,所以删除绑定不是一个选项。好的,我结束这个,因为实际上清除是以正确的方式构建的,但是垃圾收集器似乎真的花了一些时间来考虑清理win8.1机器上的东西。在“卸载”按钮上单击大约10-15次后,将进行清理。在win7机器上,清理总是在第二次单击时进行。您能显示
ViewModelBase
类代码吗?这是Galasoft.MvvmLight名称空间中的代码。很抱歉,我没有那种Galasoft
东西(不管是什么)。为了实现您的目标,您需要访问定义PropertyChanged事件的类。我将其更改为使用自己的ViewModelBase,正如预期的那样,它不会更改任何内容。很抱歉,我被误导,认为问题是由NotifyPropertyChangedEventHandler引起的,但事实并非如此。执行卸载代码时,ContentPresenter.Content
属性将被清除,但ContentPresenter.DataContext
不会被清除(至少不会立即清除-稍后在用户代码之外发生)
public MainView()
{
InitializeComponent();
MainViewModel vm = new MainViewModel();
this.DataContext = vm;
}
public MainViewModel Model { get { return this.DataContext as MainViewModel; } }
private void OnLoadChildView(object sender, System.Windows.RoutedEventArgs e)
{
this.Model.ChildViewModel = new ChildViewModel();
}
private void OnUnloadChildView(object sender, System.Windows.RoutedEventArgs e)
{
this.Model.ChildViewModel = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
public class MyListBox : ListBox
{
public MyListBox() { Debug.WriteLine("Created instance of MyListBox:" + this.GetHashCode()); }
~MyListBox() { Debug.WriteLine("destroyed instance of MyListBox:" + this.GetHashCode()); }
}
public class ViewModelBase : INotifyPropertyChanged
{
private PropertyChangedEventHandler _propertyChanged;
public event PropertyChangedEventHandler PropertyChanged
{
add
{
_propertyChanged += value;
}
remove
{
_propertyChanged -= value;
}
}
protected virtual void OnPropertyChanged(string property)
{
if (_propertyChanged != null)
{
_propertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
public class MainViewModel : ViewModelBase
{
private ChildViewModel _ChildViewModel;
public ChildViewModel ChildViewModel
{
get { return _ChildViewModel; }
set
{
if (_ChildViewModel != value)
{
_ChildViewModel = value;
OnPropertyChanged(nameof(ChildViewModel));
}
}
}
}
public class ChildViewModel : ViewModelBase
{
public ChildViewModel()
{
Debug.WriteLine("Created instance of ChildViewModel:" + this.GetHashCode());
this.Items = new List<string>(new string[] { "ABC", "DEF" });
}
~ChildViewModel()
{
this.Items = null;
Debug.WriteLine("destroyed instance of ChildViewModel:" + this.GetHashCode());
}
private IEnumerable<string> _Items;
public IEnumerable<string> Items
{
get { return _Items; }
set
{
if (_Items != value)
{
_Items = value;
OnPropertyChanged(nameof(Items));
}
}
}
}
<Application.Resources>
<DataTemplate DataType="{x:Type vm:ChildViewModel}">
<vw:ChildView/>
</DataTemplate>
</Application.Resources>
Created instance of ChildViewModel:2670961
Created instance of ChildView:2080614
Created instance of MyListBox:33397791
destroyed instance of ChildViewModel:2670961
<StackPanel>
<local:MyListBox>
</local:MyListBox>
</StackPanel>
Created instance of ChildViewModel:30767036
Created instance of ChildView:50297887
Created instance of MyListBox:5453341
destroyed instance of MyListBox:5453341
destroyed instance of ChildViewModel:30767036
Destroyed instance of ChildView:50297887