3

First of all, I have two pages called MainPage and BindPage. The MainPage can navigate to the BindPage. The BindPage have two bindings:

One is DataContext Binding in xaml:

<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">

The other binding is in the page content:

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

The definition of VM is:

    public BindingViewModel VM
    {
        get
        {
            return DataContext as BindingViewModel;
        }
    }

The BindingViewModel has just one property:

    private bool isInBuyProcess;

    public bool IsInBuyProcess
    {
        get
        {
            return true;
        }
        set
        {
            isInBuyProcess = value;
        }
    }

And also I need to show BindPage.g.cs, it contain two important classes: BindingPage_obj1_Bindings & BindingPage_obj1_BindingsTracking

BindingPage_obj1_BindingsTracking is the member of 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);
        }

The BindingPage_obj1_Bindings is initialize here:

    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;
    }

And BindingPage_obj1_BindingsTracking could not be released outside of 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;
                    }
                }
            }

After I navigate From MainPage to BindPage, and then go back with GC collection, the BindPage and BindingPage_obj1_Bindings can be released but BindingPage_obj1_BindingsTracking NOT.

If I move the DataContext binding to code-behind and write it to the Page_Loaded event, BindingPage_obj1_BindingsTracking will be released.

So, does the result mean the x:Bind could not be used with Binding?

btw, my test project here

======================Update======================

After some days, I find the result. The solution is quite simple, just call the Bindings.StopTracking() in the Page_Unloaded method, so all the one-way binding and two way binding should be released.

The solution is not a good way to implement more with MVVM because the Bindings property is private and we need to call the function in the code-behind. I don't quite understand why Microsoft generate the Bindings to private.

  • @bartlannoeye I update my question, is that enough and clear? Sorry for my pool English. – Kirito Studio Dec 10 '15 at 14:18
  • 3
    This is a known issue and has been reported before. There's another SO question about it [here](http://stackoverflow.com/questions/32581432/uwp-compiled-binding-xbind-produces-memory-leaks) (and some comments about how it's not necessarily a long term memory leak depending on the application): http://stackoverflow.com/questions/32581432/uwp-compiled-binding-xbind-produces-memory-leaks – WiredPrairie Dec 10 '15 at 14:42
  • @WiredPrairie I have updated my question, seemed the solution is not perfect but should work. – Kirito Studio Dec 18 '15 at 03:05

0 Answers0