question

EJ-5254 avatar image
0 Votes"
EJ-5254 asked WenyanZhang-MSFT commented

ContentView OnPropertyChanged not called when removing view in Android

Hi,

I need to cleanup ContentView when ContentPage is removed from stack, found some code which uses custom renderer resolver and works perfectly in iOS, but on Android only works when page is pushed and not when removed.

In ContentView code behind I've this:

 protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
 {
     base.OnPropertyChanged(propertyName);
        
     if(propertyName.Equals("Renderer", StringComparison.OrdinalIgnoreCase))
     {
         var rr = DependencyService.Get<IRendererResolver>();
            
         if(rr.HasRenderer(this))
         {
             //do setup work
         }
         else
         { 
             // do shutdown work
         }
     }
 }

When closing ContentPage which contains ContentView with this code, on Android OnPropertyChanged even is not raised at all. Any idea why it works in iOS and not Android?


dotnet-xamarinforms
· 2
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.

May I ask how did you close the ContentPage? I test with this demo , add OnPropertyChanged method in CardView, remove the cardview with OnDisappearing() mothod, it works , you can check it.

0 Votes 0 ·

CardViewXamlPage.XAML

  <StackLayout x:Name="stack">
                 <Button Text="click to remove" Clicked="Button_Clicked"></Button>
                 <controls:CardView BorderColor="DarkGray"
                                    CardTitle="Slavko Vlasic"
                                    CardDescription="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla elit dolor, convallis non interdum."
                                    IconBackgroundColor="SlateGray"
                                    IconImageSource="user.png" />
             </StackLayout>

CardViewXamlPage.cs

 protected override void OnDisappearing()
         {
             base.OnDisappearing();
             System.Console.WriteLine("OnDisappearing Clear");
             //stack.Children.Clear();
         }

CardView

 protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
         {
             base.OnPropertyChanged(propertyName);
             System.Console.WriteLine("OnPropertyChanged------");
             if (propertyName.Equals("Renderer", StringComparison.OrdinalIgnoreCase))
             {
                 System.Console.WriteLine("dosomthing");
             }
         }
0 Votes 0 ·

0 Answers