0
加載的用戶控件
我在我的MAINVIEW以下代碼(窗口)設置經由ContentPresenter
<StackPanel>
<Button Click="OnLoadChildView">Load</Button>
<Button Click="OnUnloadChildView">UnLoad</Button>
<ContentPresenter Content="{Binding ChildViewModel}"/>
</StackPanel>
和這個代碼在我ChildView(用戶控件)
<StackPanel>
<local:MyListBox ItemsSource="{Binding Items}">
</local:MyListBox>
</StackPanel>
這是的MainView的代碼
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));
}
}
}
}
我使用的應用程序資源ADRESS的觀點:
<Application.Resources>
<DataTemplate DataType="{x:Type vm:ChildViewModel}">
<vw:ChildView/>
</DataTemplate>
</Application.Resources>
當我啓動應用程序,然後單擊Load按鈕,然後卸載按鈕幾次我得到這樣的輸出:
Created instance of ChildViewModel:2670961
Created instance of ChildView:2080614
Created instance of MyListBox:33397791
destroyed instance of ChildViewModel:2670961
當我改變ChildView的XAML到:
<StackPanel>
<local:MyListBox>
</local:MyListBox>
</StackPanel>
所以祛瘀荷蘭國際集團的結合,重新開始我的了的應用程式上,單擊Load按鈕,然後卸載按鈕幾次我得到這個:
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
這樣看來,我不能真的無法擺脫WPF的NotifyPropertyChangedEventHandler,所以我的列表框和我的視圖不會被丟棄。我怎樣才能做到這一點?因爲我認爲它已經清除,所以刪除綁定不是一種選擇。
你可以顯示'ViewModelBase'類代碼嗎? –
多數民衆贊成Galasoft.MvvmLight命名空間 –
嗯,恐怕我沒有那個'Galasoft'的東西(不管它是什麼)。爲了實現您的目標,您需要訪問PropertyChanged事件定義的類。 –