title: Xamarin: The name 'x' does not exist in the current context
body: <p>I'm new to Xamarin development and I'm hopeful there's a quick fix for this seemingly easy issue.  </p>
  
  <p><strong>My Environment</strong></p>
  
  <ul>
  <li>Xamarin Studio Community on Mac 6.1.3 (build 19) </li>
  <li>Xamarin.Android 7.0.2.42  </li>
  <li>Xamarin.Ios 10.3.1.7   </li>
  <li>Xcode 8.2.1  </li>
  <li>Mac OSX 10.12.1</li>
  </ul>
  
  <p><strong>My Issue</strong></p>
  
  <p>I have an existing xaml file from my Visual Studio Online repo.  I added an <strong>x:name</strong> attribute called <em>lblSearchText</em> to a label control so I can access it in code behind.  Code behind for the <strong>xaml</strong> allows me to access this control by the name I added without issue.  However, when compiling I get </p>
  
  <blockquote>
    <p>The name ‘lblSearchText’ does not exist in the current context</p>
  </blockquote>
  
  <p>I’ve confirmed the following</p>
  
  <ul>
  <li><strong>Build Action</strong> on the XAML file is <em>EmbeddedResource</em></li>
  <li><strong>Custom Tool</strong> property on the XAML file is <em>MSBuild:UpdateDesignTimeXaml</em></li>
  </ul>
  
  <p><strong>My XAML</strong> EDIT: added full XAML</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;common:ContentPage x:Class="MyApp.SearchAuthenticatedPage"
                      xmlns="http://xamarin.com/schemas/2014/forms"
                      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                      xmlns:common="clr-namespace:MyApp.Common;assembly=MyApp.Common"
                      xmlns:commonView="clr-namespace:MyApp.Common.Views;assembly=MyApp.Common"
                      xmlns:help="clr-namespace:MyApp.Helpers;assembly=MyApp"
                      xmlns:helpersCommon="clr-namespace:MyApp.Helpers;assembly=MyApp.Common"
                      xmlns:local="clr-namespace:MyApp;assembly=MyApp"
                      xmlns:sty="clr-namespace:MyApp.Styles;assembly=MyApp"&gt;
      &lt;common:ContentPage.Resources&gt;
          &lt;ResourceDictionary&gt;
              &lt;local:DistanceToStringConverter x:Key="DistanceToString" /&gt;
              &lt;local:StringToHeaderConverter x:Key="StringToHeader" /&gt;
              &lt;local:StringToUpperConverter x:Key="StringToUpper" /&gt;
              &lt;local:IsFavoriteToBackgroundColorConverter x:Key="IsFavoriteToBackgroundColor" /&gt;
          &lt;/ResourceDictionary&gt;
      &lt;/common:ContentPage.Resources&gt;
      &lt;RelativeLayout BackgroundColor="White" Padding="0"&gt;
          &lt;common:SearchBar x:Name="searchBar"
                            Title="{helpersCommon:Translate searchBarText}"
                            ClickCommand="{Binding SearchCommand}"
                            RelativeLayout.HeightConstraint="{ConstraintExpression Type=Constant, Constant=50}"
                            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width}"
                            RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=X}"
                            RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Y}" /&gt;
  
          &lt;AbsoluteLayout IsVisible="{Binding IsMapVisible}"
                          RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Constant=-50}"
                          RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width}"
                          RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=X}"
                          RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Y, Constant=50}"&gt;
              &lt;commonView:MapView x:Name="map"
                                  WidthRequest="500"
                                  HeightRequest="500"
                                  VerticalOptions="FillAndExpand"
                                  AbsoluteLayout.LayoutBounds="1,1,1,1"
                                  AbsoluteLayout.LayoutFlags="All"
                                  DirectionLocation="{ Binding DirectionLocation}"
                                  IsVisible="{Binding IsMapVisible}"
                                  Location="{Binding CurrentLocation}"
                                  ShowDirection="{ Binding IsShowingDirection}"
                                  Source="{Binding GetMapSource}"
                                  ZoomToLocation="true" /&gt;
              &lt;Button x:Name="getDirectionButton"
                      AbsoluteLayout.LayoutBounds=".28,.33,100,30"
                      AbsoluteLayout.LayoutFlags="PositionProportional"
                      BackgroundColor="Transparent"
                      Text="" /&gt;
              &lt;Button x:Name="storeDetailsButton"
                      AbsoluteLayout.LayoutBounds=".70,.33,100,30"
                      AbsoluteLayout.LayoutFlags="PositionProportional"
                      BackgroundColor="Transparent"
                      IsVisible="false"
                      Text="" /&gt;
              &lt;Button x:Name="phoneButton"
                      AbsoluteLayout.LayoutBounds=".28,.27,100,20"
                      AbsoluteLayout.LayoutFlags="PositionProportional"
                      BackgroundColor="Transparent"
                      Text="" /&gt;
          &lt;/AbsoluteLayout&gt;
          &lt;ListView x:Name="list"
                    HeightRequest="300"
                    CachingStrategy="RetainElement"
                    Footer="{Binding .}"
                    HasUnevenRows="true"
                    IsGroupingEnabled="true"
                    IsVisible="{Binding IsListVisible}"
                    ItemsSource="{Binding FinalSearchResult}"
                    RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Constant=-47}"
                    RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width}"
                    RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=X}"
                    RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Y, Constant=47}"
                    SeparatorColor="Transparent"
                    SeparatorVisibility="None"&gt;
              &lt;ListView.FooterTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;ContentView&gt;
                          &lt;Label x:Name="lblSearchText" &lt;!--- My Control ---&gt;
                                 Margin="13, 10"
                                 HorizontalOptions="Center"
                                 IsVisible="{Binding IsEmpty}"
                                 Style="{x:Static sty:Styles.PatientListBigLabelStyle}"
                                 Text="{helpersCommon:Translate searchText}"
                                 VerticalTextAlignment="Center" /&gt;
                      &lt;/ContentView&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ListView.FooterTemplate&gt;
              &lt;ListView.GroupHeaderTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;ViewCell&gt;
                          &lt;StackLayout BackgroundColor="{Binding Favorite, Converter={StaticResource IsFavoriteToBackgroundColor}}" Padding="13,15"&gt;
                              &lt;Label Style="{x:Static sty:Styles.DarkLabelStyle}" Text="{Binding GroupName}" /&gt;
                          &lt;/StackLayout&gt;
                      &lt;/ViewCell&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ListView.GroupHeaderTemplate&gt;
              &lt;ListView.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;ViewCell&gt;
                          &lt;Grid BackgroundColor="{Binding IsFavorite, Converter={StaticResource IsFavoriteToBackgroundColor}}"
                                Padding="13,16"
                                RowSpacing="0"&gt;
                              &lt;Grid.RowDefinitions&gt;
                                  &lt;RowDefinition Height="*" /&gt;
                                  &lt;RowDefinition Height="5" /&gt;
                                  &lt;RowDefinition Height="Auto" /&gt;
                                  &lt;RowDefinition Height="Auto" /&gt;
                                  &lt;RowDefinition Height="35" /&gt;
                                  &lt;RowDefinition Height="Auto" /&gt;
                              &lt;/Grid.RowDefinitions&gt;
                              &lt;Grid.ColumnDefinitions&gt;
                                  &lt;ColumnDefinition Width="30" /&gt;
                                  &lt;ColumnDefinition Width="*" /&gt;
                                  &lt;ColumnDefinition Width="100" /&gt;
                              &lt;/Grid.ColumnDefinitions&gt;
                              &lt;commonView:FavoriteButton Grid.Row="0"
                                                         Grid.Column="0"
                                                         Margin="0,0,7,0"
                                                         ClickCommand="{Binding FavoriteChangedCommand}"
                                                         IsFavorite="{Binding IsFavorite}"
                                                         Style="{x:Static sty:Styles.FavoriteButtonStyle}" /&gt;
  
                              &lt;Label Grid.Row="0"
                                     Grid.Column="1"
                                     FontSize="{x:Static sty:Styles.VeryLargeFontSize}"
                                     Style="{x:Static sty:Styles.DarkBoldLabelStyle}"
                                     Text="{Binding ShoppingCenterName}" /&gt;
                              &lt;Label Grid.Row="0"
                                     Grid.Column="2"
                                     FontSize="{x:Static sty:Styles.VeryLargeFontSize}"
                                     Style="{x:Static sty:Styles.DarkBoldRightLabelStyle}"
                                     Text="{Binding DistanceInMilesFromLocation, Converter={StaticResource DistanceToString}}" /&gt;
                              &lt;Label Grid.Row="2"
                                     Grid.Column="1"
                                     IsVisible="{Binding IsAddressVisible}"
                                     Style="{x:Static sty:Styles.DarkLabelStyle}"
                                     Text="{Binding Address}" /&gt;
                              &lt;commonView:UnderlinedButton Grid.Row="2"
                                                           Grid.RowSpan="3"
                                                           Grid.Column="2"
                                                           HeightRequest="35"
                                                           Command="{Binding ShowDetailCommand}"
                                                           IsVisible="{Binding IsNotSomeMode}"
                                                           Style="{x:Static sty:Styles.ListUnderlinedButtonStyle}"
                                                           Text="{helpersCommon:Translate detailButtonText}"
                                                           TextAligment="TopRight" /&gt;
                              &lt;Label Grid.Row="3"
                                     Grid.Column="1"
                                     VerticalOptions="Start"
                                     IsVisible="{Binding IsAddressVisible}"
                                     Style="{x:Static sty:Styles.DarkLabelStyle}"
                                     Text="{Binding Address2}" /&gt;
                              &lt;commonView:UnderlinedButton Grid.Row="4"
                                                           Grid.Column="1"
                                                           HeightRequest="35"
                                                           HorizontalOptions="StartAndExpand"
                                                           Command="{Binding CallCommand}"
                                                           Style="{x:Static sty:Styles.ListUnderlinedButtonStyle}"
                                                           Text="{Binding Phone}"
                                                           TextAligment="TopLeft" /&gt;
                              &lt;commonView:ExtendedButton Grid.Row="4"
                                                         Grid.RowSpan="2"
                                                         Grid.Column="2"
                                                         HorizontalOptions="End"
                                                         VerticalOptions="Center"
                                                         Command="{Binding SomeCommand}"
                                                         IsVisible="{Binding IsSomeMode}"
                                                         Style="{x:Static sty:Styles.ActionButtonStyle}"
                                                         Text="{helpersCommon:Translate ListSomeButtonText}"&gt;
                                  &lt;commonView:ExtendedButton.FontSize&gt;
                                      &lt;OnPlatform x:TypeArguments="x:Double"
                                                  Android="11"
                                                  iOS="13" /&gt;
                                  &lt;/commonView:ExtendedButton.FontSize&gt;
                              &lt;/commonView:ExtendedButton&gt;
                             &lt;/Grid&gt;
                      &lt;/ViewCell&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ListView.ItemTemplate&gt;
          &lt;/ListView&gt;
      &lt;/RelativeLayout&gt;
  &lt;/common:ContentPage&gt;
  </code></pre>
  
  <p><strong>My Code Behind</strong> EDIT - added full code</p>
  
  <pre><code>using System.Linq;
  using Xamarin.Forms;
  using static MyApp.Helpers.UIHelper;
  
  namespace MyApp
  {
      public partial class SearchAuthenticatedPage : global::MyApp.Common.ContentPage
      {
          public SearchAuthenticatedPage()
          {
              InitializeComponent();
              lblSearchText.Text = "hello world!";
          }
  
          public bool IsMapShowing { get; set; }
  
          protected override void OnBindingContextChanged()
          {
              base.OnBindingContextChanged();
              var vm = BindingContext as BaseListViewModel;
              if (vm != null) {
                  if (!ToolbarItems.Any()) {
                      SetSwitchToolbarItem();
                  }
                  vm.SetSwitchToolbarItem = SetSwitchToolbarItem; 
              }
              getDirectionButton.IsVisible = false;
              storeDetailsButton.IsVisible = false;
              phoneButton.IsVisible = false;
  
              map.CalloutShownCommand = new Command(OnShowCallout);
              map.CalloutHidedCommand = new Command(OnHideCallout);
          }
  
          void OnHideCallout()
          {
              if (Device.OS == TargetPlatform.iOS) {
                  return;
              }
              getDirectionButton.IsVisible = false;
              phoneButton.IsVisible = false;
          }
  
          void OnShowCallout()
          {
              if (Device.OS == TargetPlatform.iOS) {
                  return;
              }
              getDirectionButton.Command = map.ShowDirectionsCommand;
              phoneButton.Command = map.PhoneCallCommand;
              getDirectionButton.IsVisible = true;
              phoneButton.IsVisible = true;
          }
  
          public void SetSwitchToolbarItem(bool isMapClicked = false)
          {
              var switchCommand = (BindingContext as BaseListViewModel)?.ChangeViewCommand;
              var switchItem = ToolbarItems.FirstOrDefault(i =&gt; i.Priority == 0);
              if (switchItem != null) {
                  switchItem.Text = isMapClicked ? Localize("titleListView") : Localize("titleMapView");
                  switchItem.Command = switchCommand;
              }
              else {
                  ToolbarItems.Add(new ToolbarItem { Text = Localize("titleMapView"), Command = switchCommand });
              }
          }
      }
  }
  </code></pre>