Once, when developing an app we were navigating with ContentViews on one page. I can’t remember what was the reason for doing so but we needed to do so. What’s more, we were somehow navigating between those views. Hence, we wanted to do something in OnAppearing method. Here we came to the problem: ContentViews don’t have a lifecycle. We didn’t solve it then – we put the needed logic somewhere else so it worked.

Now, almost 3 years later I am struggling with a similar problem. This time I’ve decided I need to figure out how to do it properly.

Assumptions

Having in mind the above, I’d like to have OnAppearing & OnDisappearing methods. Further more, I’d like to be able to add ToolbarItems from my ContentView (right now it is not possible).

Problem statement

So I started working. Firstly I was focused on adding ToolbarItem. “It shouldn’t be that hard” – I thought. I was wrong. Again.

via GIPHY

So I did what developers can do pretty well. I googled “ContentView ToolbarItem” . The results where shown – something from StackOverflow & Xamarin forums. Unfortunately, each solution was suggesting you should play around with Parent property of your ContentView get ContentPage and add ToolbarItems. So I followed that approach.

It happened that my ContentView (thanks to some third party library that we were using) was not added directly into ContentPage. The view hierarchy was like:

ContentPage->Grid->[...]->Grid->ContentView

As you may assume playing with Parent was an option but it wasn’t reusable & wasn’t that nice. So I took my (spare) time and I thought about how to solve it in a nicer manner.

Playing around with OnPropertyChanged

Having some experience in Xamarin.Forms I thought that maybe we can use OnPropertyChanged(string propertyName = null). I debugged my app and saw that this method is being invoked with some interesting properties like Renderer, IsVisible, Parent etc.

Not thinking much I created virtual void OnViewAppeared() and virtual void OnViewDisappeared() methods. But I still haven’t solved my problem with Toolbar. As I didn’t want to expose the parent ContentPage I thought that creating virtual void OnToolbarAvailable(IList<ToolbarItem> toolbar) the method might be a good idea. If you want to add something to the toolbar you simply need to override this method and add your items to toolbar. Simple as that.

I’ve added some logic to that method, spiced it with some ifs and local fields and I was good to go. Almost good, because the tests have shown that once you add something from ContentView to ContentPage it is still there unless you remove it. So I’ve added cleaning the toolbar when ContentView disappears.

Here is the code I came up with:

As you can see there is no complex logic in it. Only few ifs and several flags.

Summary

I’ve checked the code in two solutions and it looks like it’s working. I can’t promise it will work in your project. What I can promise though is that you can have something similar. I would like to encourage you to play with OnPropertyChanged method and see what you can take from it.

Sometimes you don’t need to go native, create renderers etc. All is required is some creativity. Good luck!

Code available on Github. But it is not (yet) a part of Nuget. Will change in next days

[mc4wp_form id=”497″]