Bug 43911 - Android renderers timing issue with OnAppearing Method and Urho Surface
Summary: Android renderers timing issue with OnAppearing Method and Urho Surface
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.2
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2016-08-31 18:15 UTC by Reiss Cashmore
Modified: 2017-05-17 19:03 UTC (History)
3 users (show)

Tags: ac
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 43911 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Reiss Cashmore 2016-08-31 18:15:07 UTC
I have a Custom Renderer for a TabbedPage and the Tabbed Page has Child Content Pages just as a normal Tabbed Page would. I have Uhro Surface controls in some of those Content Pages. When I first open the Activity and I try to Show the Urho surface when the OnAppearing method is called on the Child Content Page, it throws a: System.InvalidOperationException: Platform implementation is not referenced.

This works fine if I do not use a custom Renderer for my Tabbed Page. It would appear the OnAppearing event fires before the Custom Renderers have finished executing. 

Is there something else I could be using to ensure the Renderer has finished before trying to Show my Urho Surface. A private variable / event "hasAppeared" is referenced in this post: https://forums.xamarin.com/discussion/29714/is-there-an-event-that-fires-after-a-page-is-showed Can I utilise this or something similar?

SanderSchutten has also come across this behaviour suggests to use a Task.Delay(1000) after the OnAppearing event fires before calling the Show on the Urho surface. This works however is not a production grade fix. 

See SanderSchutten post From this link: https://forums.xamarin.com/discussion/67735/android-renderers-timing-issue-when-using-masterdetailpage-with-formsappcompatactivity

This is a quote of SanderShuttens behaviour:

"This is a rather tricky one and it took me a while to pinpoint it to a specific set of conditions. Not sure it it's a bug or that I should do something differently.

I was playing around with UrhoSharp in combination with Xamarin.Forms in my application when it gave me the exception:
System.InvalidOperationException: Platform implementation is not referenced

After a bit of digging through the code of UrhoSharp I noticed that this exception occurred because when the OnAppearing() method was called, the renderer hadn't been executed yet. However, if I tried the UrhoSharp example solution, it did work as expected.

Eventually it appears there's something going on with the Android renderers being executed when the following conditions are met:

FormsAppCompatActivity is used
MasterDetailPage is used
The app navigates to the MasterDetailPage; it isn't the first page in the app
The control in question lives on the Detail page of the MasterDetailPage
The control is expected to be rendered and available when the OnAppearing() is called
I've created a sample to highlight this issue, which can be found here.

If you run the sample unmodified, you get the exception as stated at the beginning of this post.
If you change the sample to use FormsApplicationActivity instead of FormsAppCompatActivity, it works
If you change the sample to use the MasterDetailPage as start page, it works
If you change the sample to navigate directly to the UrhoPage instead of the MasterDetailPage, it works
If you add a Task.Delay(1000) in the OnAppearing() method before the call to StartUrhoApp(), it works
Because of point 5 it makes me think there's some race condition going on and at the time OnAppearing() is called, the renderer is still being executed.
If this is to be expected of the OnAppearing() method, I'd like a safer way to know when the renderer is finished executing. If this is unexpected behavior, than what could be causing it? "
Comment 1 Chris King 2016-10-06 17:18:13 UTC
Throws Platform implementation is not referenced on master.
Comment 2 Egorbo 2017-05-17 19:03:09 UTC
looks like the issue is UrhoSharp specific - it was not a good idea to expect to have an inited custom control inside the UrhoSurface.Show method (what if it's called before OnAppearing?) - I've just pushed a fix to UrhoSharp master - https://github.com/xamarin/urho/commit/a1194e9223a23aa01d3c09636c60043b8997aec9#diff-e8337a87837100586723aaedea568cc4L13