Bug 48680 - FindByName Functionality Failure
Summary: FindByName Functionality Failure
Status: RESOLVED NOT_REPRODUCIBLE
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.3
Hardware: PC Windows
: Low normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-12-02 02:43 UTC by mwg@thoughts.net
Modified: 2017-06-21 16:15 UTC (History)
8 users (show)

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


Attachments
Screenshot-Row1 assigned (88.48 KB, image/png)
2017-01-25 19:34 UTC, Samantha Houts [MSFT]
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 NOT_REPRODUCIBLE

Description mwg@thoughts.net 2016-12-02 02:43:20 UTC
From a generated "global::Xamarin.Forms.ContentPage" xaml.g.cs file base on Xamarin.Forms v2.3.3.168, in the call to InitializeComponent(), during the initialization of the page, the FindByName methods fails to find the named XAML elements resulting in a "null" value for all named elements beginning with the LayoutRoot.

        [System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
        private void InitializeComponent() {
            this.LoadFromXaml(typeof(HomePage));
            LayoutRoot = this.FindByName<global::Xamarin.Forms.Grid>("LayoutRoot");
            Col0 = this.FindByName<global::Xamarin.Forms.ColumnDefinition>("Col0");

The source XAML was

  <ContentPage.Content>
    <AbsoluteLayout>
        <!--LayoutRoot is the root grid where all page content is placed-->
        <Grid x:Name="LayoutRoot" HorizontalOptions="Center" VerticalOptions="Center" Opacity="100" Margin="0" Padding="0">

          <Grid.ColumnDefinitions>
              <ColumnDefinition x:Name="Col0">
              </ColumnDefinition>
Comment 1 Samantha Houts [MSFT] 2016-12-02 18:42:27 UTC
Thank you for taking the time to submit the bug. We tried to reproduce the issue you reported but were unable given the description. If you could please attach a reproduction to the bug by starting with a clean Xamarin.Forms project and adding just the code necessary to demonstrate the issue, we would very much appreciate it.  

For your convenience, we have created some reproduction best practices viewable here: https://gist.github.com/jassmith/92405c300e54a01dcc6d  

Warm regards, 
Xamarin Forms Team
Comment 2 mwg@thoughts.net 2016-12-03 11:27:32 UTC
The requested project solution (23MB) can be downloaded using this link: http://public.thoughts.net/Downloads/Sentry.zip.

The solution provides two examples of the expected functionality, working and error producing. The out of zip file configuration represents working. Changing the MainPage constructor parameter to "new HomePage()" by switching the commented code lines in the App.cs file and subsequently recompiling produces the error producing version. 

The solution targets a WinPhone v8.1 implementation for debugging so the appropriate emulator is needed.

I am performing page element addition testing to the MainPage.xaml file today to determine if a specific element is at fault.

I am available until 5PM EST today if you need to contact me.

Marc
Comment 3 mwg@thoughts.net 2016-12-03 15:46:52 UTC
I am finding that during the creation of the MainPage the exception
{"Object of type 'Xamarin.Forms.ColumnDefinition' cannot be converted to type 'Xamarin.Forms.View'."} is being generated.
Comment 4 mwg@thoughts.net 2016-12-04 13:20:13 UTC
Changed directions. Restarted solution from scratch and slowly adding code. So far so good. FindByName seems to be working. 

Major change though is not using "MainPage = new NavigationPage(new Sentry.MainPage())" but just "MainPage = new Sentry.MainPage()" statement.

When using previous, I encounter:

"Exception = {"XAML parsing failed."}"
"The property 'ToolbarBackground' was not found in type 'Xamarin.Forms.Platform.WinRT.PageControl'. [Line: 18 Position: 15]"

'ToolbarBackground' is not in my page XAML.
Comment 5 RockStar 2016-12-22 15:47:10 UTC
I have this error :

"The property 'ToolbarBackground' was not found in type 'Xamarin.Forms.Platform.WinRT.PageControl'. I use Windows Phone 8.1 with Xamarin.Forms 2.3.3.175  Any ideas?
Comment 6 Jonathan Derrough 2016-12-30 10:33:03 UTC
Same as RockStar here.

I have a working Xamarin Forms PCL project (2.3.3.175) on iOS/Android and am now trying to make it run on WP 8.1, getting the "The property 'ToolbarBackground' was not found in type 'Xamarin.Forms.Platform.WinRT.PageControl'" error message wrapped in an unhandled exception.

Did a bit of tinkering, replaced the App.cs init code with that of the template:

"var content = new ContentPage
{
    Title = "XamarinTest",
    Content = new StackLayout
    {
        VerticalOptions = LayoutOptions.Center,
        Children = {
            new Label {
                HorizontalTextAlignment = TextAlignment.Center,
                Text = "Welcome to Xamarin Forms!"
            }
        }
    }
};
MainPage = new NavigationPage(content);"

Still crashes, but not wrapping the content page in a navigation page does not crash:

"MainPage = content;"

A fresh project from the Xamarin PCL template runs on a WP 8.1 without a hitch even though the content page is wrapped in a navigation page.

The project I'm porting from iOS/Android uses a number of dependencies:
Telerik UI for Xamarin Forms
FFImageLoading
GalaSoft.MVVMLight
ImageCircle
Newtonsoft.Json
PCLCrypto
PCLStorage
Plugin.Connectivity
Plugin.Media
Plugin.Permissions
Plugin.Share
Version.Plugin
... just to name a few

So I guess it can come from one of them, or not, I'm really at a loss here. Any pointers would be greatly appreciated.
Comment 7 Jonathan Derrough 2017-01-04 13:07:22 UTC
So, here is a workaround that did it for me:

1. create a new solution of the same kind (in my case Xamarin Forms PCL); make sure the base WinPhone project works

2. add all your old solution files and references/NuGet packages (I would suggest to copy your files to the new solution's file structure); make sure it still works

3. replace the WinPhone project in your old solution by the new one (you might need to edit the .csproj file with the right project name and ID, and if you copied your files rather than reference them from a different file structure you should be set)

This doesn't give us a clue how the project broke in the first place, or what the issue would. I'm definitely interested in understanding that as it looks like it might happen again.

I also noted that the old WinPhone project had Git issues (loose objects) that disappeared after going through the above steps, not sure if it's relevant.
Comment 8 RockStar 2017-01-05 09:49:47 UTC
I have this bug with Xamarin Forms 2.3.3.175. Version 2.3.3.168 is work ok.
Comment 9 Samantha Houts [MSFT] 2017-01-06 01:30:57 UTC
I was unable to reproduce the described issue using the sample provided in Comment 2. 

I interpret Comment 4 to mean that the issue only occurs with 2.3.3.168 when using a NavigationPage. The following comments seem to indicate that upgrading to 2.3.3.175 while using a NavigationPage does NOT cause the original error but instead causes a new error regarding ToolbarBackground being missing.

I have updated the solution from Comment 2 to 2.3.3.175, and I do not see any exceptions. 

Can anyone experiencing this new issue please provide a reproduction sample?

Warm regards,
Xamarin Forms Team
Comment 10 RockStar 2017-01-06 06:43:24 UTC
I get this error when created project for Windows Phone 8.1 and run it on Windows 10. For Windows 8.1 it works ok. I updated Xamarin Forms to 2.3.3.180
but I get new error "An item with the same key has already been added" in InitializeComponent. Do you need demo project this problem?
Comment 11 mwg@thoughts.net 2017-01-24 11:56:24 UTC
Finally back to this issue which continues even though there have been several update releases to the NuGet packages involved. Placing a break in InitializeComponent, running and walking the steps at that point, none of the form controls sought by FindByName return values other than null. None of the named controls can be reference from the code behind or from using FindByName in the code behind.

I created a new Cross Platform forms based client app (WinPhone only) which demonstrates this problem. It does have the NuGet packages that I was using installed, but is otherwise is a very basic sample. You can download it from here: 

    http://public.thoughts.net/IdentityCheckpoint/Sentry.zip
Comment 12 Stephane Delcroix 2017-01-24 14:54:27 UTC
I tested your Xaml file (not your project) and all the properties are correctly assigned (tested `LayoutRoot` and `Col0`).

But, as you're defining an empty Content to your ContentPage at the end of the Xaml, they could point to elements that will never be rendered.
Comment 13 mwg@thoughts.net 2017-01-24 18:52:50 UTC
Even if I put the grid inside the <ContentPage.Content> section, FindByName doesn't work. I had found examples were the grid was not inside the <ContentPage.Content> section. Hence the experiment. Neither worked. Place the grid inside the section yourself and test. I would be glad to do a GoToMeeting and share my workspace for testing. I am available until 5PM EST today.
Comment 14 mwg@thoughts.net 2017-01-25 17:00:14 UTC
I am available for debugging if desired for the remainder of today, all day tomorrow or Friday. I need to resolve this as it was working and I designed around the this function to scale my screens to a device so it would have the same look and feel regardless of the device.
Comment 15 Samantha Houts [MSFT] 2017-01-25 19:34:50 UTC
Created attachment 19481 [details]
Screenshot-Row1 assigned

@mwg@thoughts.net

I've tested your project on an 8.1 emulator and a 10 emulator, and I am not seeing any null values for the properties in InitializeComponent. They are all assigned. I've attached a screenshot showing that after Row1 has been assigned and the breakpoint has moved to Spacer1, Row1 is a non-null RowDefinition.

Please try your project on a different emulator or on a device if possible. I am wondering if this is an environmental issue, or possibly a corrupt emulator image.
Comment 16 mwg@thoughts.net 2017-01-25 21:18:32 UTC
I am not using an emulator, just a dedicated WinPhone for development. I guess I could reimage the device.
Comment 17 mwg@thoughts.net 2017-01-26 14:52:33 UTC
Spent the evening attempting to use a WinPhone emulator multiple times without success; advised VM Services not available. Followed numerous articles trying to determine problem.  Interesting since I have used the Azure emulator for server side debugging on this machine with VS.

Decided to reset the physical WinPhone and reinstall Windows 8.1. In process... Hope that fixes the problem. Wondering what the issue was and how that might impact actual deployments to client devices.
Comment 18 mwg@thoughts.net 2017-01-26 18:37:59 UTC
Finished the process and tested using example successfully.

Conclusion, something on the device (settings/libraries) was preventing access to the necessary functions which were not being updated by the new deployment of the application. What can we do to establish a baseline to compare to if the problem repeats?
Comment 19 Stephane Delcroix 2017-01-26 19:44:01 UTC
closing as it solved
Comment 20 mwg@thoughts.net 2017-01-27 01:41:10 UTC
Returning to the actual solution, built and deployed to same device, the problem reappeared. 

Looking at the .g.cs files of the two solutions, found a difference that may be part of the issue as using statements are not present in the generated file of the not working solution as well as the presence of a [] global attribute. Why?

Working
************************************************************************
namespace Sentry {
    using System;
    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;
    
    
    public partial class PCLMain : global::Xamarin.Forms.ContentPage {
        
        [System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
        private global::Xamarin.Forms.Grid LayoutRoot;

************************************************************************

Not Working
************************************************************************
namespace Sentry {
    
    
    [global::Xamarin.Forms.Xaml.XamlFilePathAttribute("C:\\Projects\\IdentityCheckpoint\\Sentry\\Sentry\\Sentry\\PCLMainPage.xaml")]
    public partial class PCLMainPage : global::Xamarin.Forms.ContentPage {
        
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
        private global::Xamarin.Forms.Grid LayoutRoot;
************************************************************************
Comment 21 mwg@thoughts.net 2017-01-27 16:00:24 UTC
Tried an experiment: Replaced the main page XAML/CS of the failing actual solution with that of the working sample that you tested. The FindByName function in the InitializeComponent method failed again. What was interesting was that generated .g.cs code had the issues noted above: not including the using statements and having the global attribute. i.e.

************************************************************************
namespace Sentry {
    
    
    [global::Xamarin.Forms.Xaml.XamlFilePathAttribute("C:\\Projects\\IdentityCheckpoint\\Sentry\\Sentry\\Sentry\\PCLMainPage.xaml")]
    public partial class PCLMainPage : global::Xamarin.Forms.ContentPage {
        
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
        private global::Xamarin.Forms.Grid LayoutRoot;
************************************************************************
Comment 22 mwg@thoughts.net 2017-01-27 17:01:44 UTC
Tried another experiment. Ran the working sample solution again. It failed. Looking at the applications on the device, saw that there was to applications present, the working sample and the actual solution. I removed both. I then re-ran the sample. It worked. Something is being changed on the device, setting/library, that is used by both and not being replaced/update during deployment. 

Back to an earlier question: What can we do to establish a baseline to compare to if the problem repeats? 

Or, how do I debug this.
Comment 23 mwg@thoughts.net 2017-01-27 22:08:39 UTC
It appears that a device reboot is necessary after removing the failing versions of either the sample or actual solution to enable the working sample to be successful, supporting in part that a setting might be involved. Should I take this to the VS team?
Comment 24 mwg@thoughts.net 2017-01-27 23:07:25 UTC
Experiment 3... I took the working example, replaced the main page XAML/CS files with the main page XAML/CS files of the actual solution. Made adjustments to code behind to enable building. Cleaned and rebuilt the modified example solution. Tested. Failed. Removed the app from the device and rebooted. Re-Tested (note no rebuild involved), just deploy. Success. FindByName working.
Comment 25 mwg@thoughts.net 2017-01-27 23:28:38 UTC
Interesting... The .g.cs file for the main page has the using statements and doesn't have the [Global] attribute before the class name.
Comment 26 mwg@thoughts.net 2017-02-10 17:49:22 UTC
Bug was reported to Microsoft support as [REG:117013015242026] "Inconsistant Generation of ".g.cs" Files Resulting in FindByName Errors" and has duplicated by David Greene <v-gredav@microsoft.com> in a provided copy of the solution and should be working with the Xamarin support team to resolve.
Comment 27 proksi 2017-03-13 10:41:48 UTC
I may be completely wrong, but I think I had similar issues, ever since upgrading to XF version 2.3.3.180...and the issue appeared only when the app was built in Release, in Debug it worked just fine, which was ok to test but couldn't publish...I'll check if the issue continues in 2.3.3.193 or later now.
Comment 28 David Ortinau [MSFT] 2017-06-21 16:15:49 UTC
We haven't been able to reproduce this issue. From investigation it doesn't appear to be anything we can see to address in Xamarin.Forms. 

If this is reproducible for someone, please provide a minimal project that demonstrates the issue with explicit steps and full environment details for  us to reopen the issue and investigate.