question

Journeyman42-5521 avatar image
0 Votes"
Journeyman42-5521 asked JarvanZhang-MSFT commented

Xamarin Forms Content View - unsubscribing from OnPropertyChanged when view unloads

I have a page in which I load a number of content views

One of those views has a binding context which is different to that of the parent page

Because the view is dynamic (loading comments, allowing users to like / unlike and post), it has OnBindingContext change set

However, when the user returns to another page, everything is retained in memory because the PropertyChanged event is still linked

       _my_Vm_PropertyChanged += PropertyChanged

There is no OnDisappear or Unload method I can see

Do I have no choice but to move all my display logic to the ViewModel to which the parent page has binding context set?

dotnet-xamarinforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT commented

when the user returns to another page, everything is retained in memory because the PropertyChanged event is still linked

Hi, Journeyman42-5521. How did you set binding for the contentView in xaml? I tested a basic demo to test the function, it works fine. Could you post the related code to reproduce the issue?

Here is the related code about my test, you could refer to:

Custom contentView.xaml:

<StackLayout>
    <Label Text="{Binding TheText}" HorizontalOptions="CenterAndExpand" />
</StackLayout>

Page1

<StackLayout>
    <Button Text="Navigation" Clicked="Button_Clicked"/>
    <local:View1 />
</StackLayout>

public partial class Page1 : ContentPage
{
    public string TheText { get; set; }
    public Page1()
    {
        InitializeComponent();

        TheText = "testing for the binding of contentView";
        BindingContext = this;
    }

    private void Button_Clicked(object sender, EventArgs e)
    {
        Navigation.PushAsync(new Page2());
    }
}

Page2

<StackLayout>
    <local:View1 />
</StackLayout>

public partial class Page2 : ContentPage
{
    public string TheText { get; set; }
    public Page2()
    {
        InitializeComponent();

        TheText = "the binding text in Page2.xaml.cs";
        BindingContext = this;
    }
}
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The binding is set in code behind because as mentioned it needs to update repeatedly when the user interacts

     protected override void OnBindingContextChanged()

     {
         _vm = BindingContext as ViewModels.LikesViewModel;

         _vm.PropertyChanged += _vm_PropertyChanged;

     }

     private void _vm_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
     {
         //System.Diagnostics.Debug.WriteLine("Debug: property changed: " + e.PropertyName);
     }

0 Votes 0 ·

What's the '_vm' in your code? You could remove the PropertyChanged method for the '_vm' in the OnDisappearing method of the page.


protected override void OnDisappearing()
{
    base.OnDisappearing();
    _vm.PropertyChanged -= _vm_PropertyChanged;

}


0 Votes 0 ·

That's the problem, it is not a page, it is a content view

I have a content page, with four views

For most of those views, the data they display is adequate to be shown on init

Because of the content in Comments being dynamic, need to have its own view model

That is why I set OnPropertyChanged in the view, as is clearly allowed by the Xamarin framework

What doesn't appear to be possible is to unset it because there is no way of trapping when a ContentView is unloaded

0 Votes 0 ·
Show more comments