Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 75885

Reusing custom renderers

$
0
0

Hi all,
In my Xamarin.Forms application I use NavigationPage with stack of ContentPages to display my content. It's similar, for example, to typical mail application: it has some list of items and when user taps one of the items in a list I open new page with the details of this item. Than user can navigate back to list and open another item. Actual application is much more complex with deeper stack of pages, but mail app example is good enough.
The details page in my case is pretty complicated and has many controls, so I experience some performance issues opening it.
I tried to overcome these issues with the following solution: when user leaves the details page, I put it in 'free list' of pages which I maintain and when they tap another item I retrieve the page from 'free list', change its BindingContext to the new item and display it - rather than creating new ContentPage with the whole bunch of controls. I use data binding for all my controls, so this solution works as expected. Actually, it even works much faster than the original version... But still not good enough.
While profiling this scenario I noticed that the main problem is with custom renderers which I have for many of my controls.
I do reuse Xamarin controls when re-displaying the page, but still in the debugger I see that I come to OnElementChanged function of custom renderer and its Control property is null. So each time I want to display my page, the renderers and their native controls are created - over and over again. Also, each time when I leave the page (and set its BindingContext to null) I see that renderers of all my controls are disposed. This is obviously right in general case for freeing the memory, but inappropriate in my scenario.

So, the question is - how can I reuse custom renderers for my Xamarin controls and avoid disposing and recreating their native counterparts?
In several threads here I read about Xamarin optimizations for ViewCells inside ListView. They said they do reuse renderers for the cells - to solve exactly the same problem while scrolling the list. How this is achieved?

Thanks in advance and sorry for a long question.


Viewing all articles
Browse latest Browse all 75885

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>