首先,我有两个页面叫做MainPage和BindPage。 MainPage可以导航到BindPage。 BindPage有两个绑定:
一个是xaml中的DataContext绑定:
<Page x:Class="MvvmlightTest.View.BindingPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MvvmlightTest.View"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:cvt="using:MvvmlightTest.Converter"
DataContext="{Binding BindingVM, Source={StaticResource Locator}, Mode=OneWay}"
Unloaded="Page_Unloaded">
另一个绑定在页面内容中:
<Grid Background="#7F919191"
Visibility="{x:Bind VM.IsInBuyProcess, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverter}}">
<ProgressRing Width="100"
Height="100"
HorizontalAlignment="Center"
VerticalAlignment="Center"
IsActive="True" />
</Grid>
VM的定义是:
public BindingViewModel VM
{
get
{
return DataContext as BindingViewModel;
}
}
BindingViewModel只有一个属性:
private bool isInBuyProcess;
public bool IsInBuyProcess
{
get
{
return true;
}
set
{
isInBuyProcess = value;
}
}
我还需要显示BindPage.g.cs,它包含两个重要的类:BindingPage_obj1_Bindings&amp; BindingPage_obj1_BindingsTracking
BindingPage_obj1_BindingsTracking是BindingPage_obj1_Bindings的成员
public BindingPage_obj1_BindingsTracking bindingsTracking;
public BindingPage_obj1_Bindings()
{
this.bindingsTracking = new BindingPage_obj1_BindingsTracking(this);
Tk = new WeakReference<BindingPage_obj1_BindingsTracking>(this.bindingsTracking);
}
BindingPage_obj1_Bindings在这里初始化:
public global::Windows.UI.Xaml.Markup.IComponentConnector GetBindingConnector(int connectionId, object target)
{
global::Windows.UI.Xaml.Markup.IComponentConnector returnValue = null;
switch(connectionId)
{
case 1:
{
global::Windows.UI.Xaml.Controls.Page element1 = (global::Windows.UI.Xaml.Controls.Page)target;
BindingPage_obj1_Bindings bindings = new BindingPage_obj1_Bindings();
returnValue = bindings;
bindings.SetDataRoot(this);
bindings.SetConverterLookupRoot(this);
this.Bindings = bindings;
element1.Loading += bindings.Loading;
}
break;
}
return returnValue;
}
BindingPage_obj1_BindingsTracking无法在BindingPage_obj1_Bindings之外发布
private void StopTracking()
{
this.bindingsTracking.ReleaseAllListeners();
this.initialized = false;
}
private void ReleaseAllListeners()
{
UpdateChildListeners_VM(null);
}
private void UpdateChildListeners_VM(global::MvvmlightTest.ViewModel.BindingViewModel obj)
{
if (obj != cache_VM)
{
if (cache_VM != null)
{
((global::System.ComponentModel.INotifyPropertyChanged)cache_VM).PropertyChanged -= PropertyChanged_VM;
cache_VM = null;
}
if (obj != null)
{
cache_VM = obj;
((global::System.ComponentModel.INotifyPropertyChanged)obj).PropertyChanged += PropertyChanged_VM;
}
}
}
从 MainPage 导航到 BindPage ,然后返回GC集合后,可以释放BindPage和BindingPage_obj1_Binding但BindingPage_obj1_BindingsTracking NOT
如果我将DataContext绑定移动到代码隐藏并将其写入Page_Loaded事件,则将释放BindingPage_obj1_BindingsTracking。
那么,结果是否意味着x:Bind不能与Binding一起使用?
btw,我的测试项目here
======================更新======================
几天后,我找到了结果。解决方案非常简单,只需在 Page_Unloaded 方法中调用 Bindings.StopTracking(),因此应该释放所有单向绑定和双向绑定。
解决方案不是使用MVVM实现更多功能的好方法,因为Bindings属性是私有的,我们需要在代码隐藏中调用该函数。我不太清楚为什么Microsoft会将绑定生成私有。