roerjo
roerjo

Reputation: 830

Name from XAML file not in current context in cs file

I'm running Visual Studio 2015 Enterprise and I am trying to build a Xamarin project. I created a Blank XAML App project. I went went to MainPage.xaml file and added an x.Name to the boilerplate Label like so:

enter image description here

In the MainPage.xaml.cs file I attempt to change the text of that Label by referencing that name hello:

enter image description here

VS does not seem to like this as I get an error:

The name "hello" does not exist in the current context.

enter image description here

How can I access the names in the XAML file? I've tried cleaning the solution and rebuilding with no success.

Upvotes: 0

Views: 1974

Answers (2)

Grzegorz Gabrychowicz
Grzegorz Gabrychowicz

Reputation: 81

As @Sven-Michael Stübe noticed first you need to swap your x.Name declaration (which is incorrect) with x:Name.

Then, right click on your project and click build (or ⌘K shortcut on Mac) to allow the complier see your name declaration. The red underline should disappear afterwards.

Upvotes: 0

Sven-Michael Stübe
Sven-Michael Stübe

Reputation: 14760

You have to use x:Name instead of x.Name. x is the namespace you have declared with xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" and if you want to use something declared in this namespace, you have to use x followed by a colon and than the class or attribute name.

<Label x:Name="hello" 
       Text="Welcome to Stackoverflow" 
       VerticalOptions="Center" 
       HorizontalOptions="Center"/>

Upvotes: 3

Related Questions