Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Code-behind x:Name not linking properly #2574

Closed
jhumphrey1 opened this issue May 1, 2018 · 6 comments · Fixed by #2582
Closed

Code-behind x:Name not linking properly #2574

jhumphrey1 opened this issue May 1, 2018 · 6 comments · Fixed by #2582
Assignees
Projects

Comments

@jhumphrey1
Copy link

Description

Cannot access named components from code-behind

Steps to Reproduce

  1. Create a new blank Xamarin Forms project
  2. Add 'x:Name="myTextLabel" ' to the default "Welcome to Xamarin.Forms" label, and 'x:Name="myContentPage" ' to the default ContentPage
  3. Try and reference them in the code-behind

Expected Behavior

Should run as expected

Actual Behavior

myTextLabel gives 'Not Available' error (shown in image)
myContentPage gives standard "myContentPage does not exist in current context"
xamarin-error

Trying to run the code with just the myTextLabel referenced works as expected. Trying to run it with myContentPage referenced does not.

Basic Information

  • Version with issue:
  • Last known good version: N/A
  • IDE: Visual Studio
  • Platform Target Frameworks:
    • iOS: 11.3
    • Android: 8.1
  • Android Support Library Version: v27.0.2
  • Nuget Packages: Xamarin.Forms v2.5.1.444934
  • Affected Devices: Note 8, iPhone X

Screenshots

Here is the xaml, all blocked text is the same (Namespace name)
xamarin-error-2

@StephaneDelcroix
Copy link
Member

  1. textLabel getting underlined is an intelissense issue. A lot of improvement on this side are coming very soon...
  2. we indeed do not generate fields for x:Names declared on the root XAML element. But we properly register them to the namescopes, so they can be used in {x:Reference} markups.

@StephaneDelcroix
Copy link
Member

I'm still wondering why one needs a field to be created for a x:Name on the root 🤷‍♂️

StephaneDelcroix added a commit that referenced this issue May 2, 2018
StephaneDelcroix added a commit that referenced this issue May 2, 2018
@samhouts samhouts added this to In Progress in v3.1.0 May 2, 2018
v3.1.0 automation moved this from In Progress to Done May 2, 2018
Triage automation moved this from New to Closed May 2, 2018
@samhouts samhouts added this to Done in v3.6.0 May 4, 2018
@samhouts samhouts removed this from Done in v3.6.0 May 7, 2018
@samhouts samhouts removed this from Closed in Triage Jun 12, 2018
@assanesuzy
Copy link

This is still an issue. This happened to specific file. After updating VS for mac to 8.9.5 (build 4) it started acting up. I can't assign names to any controls other than the ones that already had a name prior to the update. I had to manually add the name in the xaml.g.cs file. after restarting, the file was generated without the name and I was back at square one

Screen Shot 2021-04-13 at 10 59 42 AM

Screen Shot 2021-04-13 at 10 59 52 AM

@restorepro
Copy link

Yes, i am having this issue still while trying to access the mediaElement control by name in the code behind. It happens often for me with various control. I usually delete obj/bin folders and restart and the problem goes away but the mediaElement x:name doesn't want to work...

@asavalle
Copy link

asavalle commented May 9, 2023

I am also still having this issue. It seems to come and go. I have several other View's in Xamarin that I am able to properly reference in the code behind. But one class in particular keeps having this issue. I tried to use the name anyway, since it was said to be an Intellisense issue, but it will not let me proceed.
This is very aggravating and seems to not be a priority at all for Microsoft.

@agostino-pique
Copy link

agostino-pique commented Apr 18, 2024

This is unfortunately still an issue on MAUI too.
Just like @asavalle some files are properly referenced but in anothers the issue always presents itself.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
v3.1.0
  
Done
Development

Successfully merging a pull request may close this issue.

6 participants