Bug 44870 - Trying to access properties of native ToggleSwitch in UWP renderer causing errors on navigate away
Summary: Trying to access properties of native ToggleSwitch in UWP renderer causing er...
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Windows ()
Version: 2.3.2
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-09-29 11:14 UTC by Jan Nepras
Modified: 2016-09-30 17:53 UTC (History)
3 users (show)

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


Attachments
The project replicating the bug. There is Main Page and SecondPage. Switch Renderer is on SecondPage. If you click on hardware bach button, then you are not able to get bacj to MainPage. (395.84 KB, application/x-zip-compressed)
2016-09-29 11:14 UTC, Jan Nepras
Details


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 on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Jan Nepras 2016-09-29 11:14:20 UTC
Created attachment 17799 [details]
The project replicating the bug. There is Main Page and SecondPage. Switch Renderer is on SecondPage. If you click on hardware bach button, then you are not able to get bacj to MainPage.

Repo steps:

1/ Run project on UWP
2/ Click the button with text "Second Page"
3/ Click hardware back button

It stucks on Second Page and the page is empty without switch.

In output window have those errors:
Exception thrown: 'System.IO.FileNotFoundException' in mscorlib.ni.dll
Exception thrown: 'System.NullReferenceException' in SwitchRendererUWP.UWP.exe
Exception thrown: 'System.NullReferenceException' in mscorlib.ni.dll
Exception thrown: 'System.AggregateException' in Xamarin.Forms.Core.dll


If you hit hardware back button again you can get to Main Page. 

I tested out this bug with Xamarin.Forms stable 2.3.2.127 and prerelease 2.3.3.152-pre2.

Tested device: Nokia Lumia 550, +emulators

Let me know if you need additional info.
Comment 1 Samantha Houts [MSFT] 2016-09-30 17:53:16 UTC
The error is in CustomSwitch_UWP, OnElementChanged. UWP itself is a bit more aggressive about cleaning up views that are no longer visible, so when the Back button is pressed, it disposes SecondPage and all of its controls, triggering the OnElementChanged event, setting the Control to null. It's good practice to do a null check on the Control (or on e.NewElement) before accessing its properties in OnElementChanged due to this scenario (you'll see that we do this in all of our renderers, too).

If this does not resolve your issue, please feel free to reopen this report with additional information. 

Warm regards,
Xamarin Forms Team