title: How to get controls described in XAML file in a Xamarin Form project?
body: <p>I can't properly get the control described in XAML file. I gave button in XAML file a <code>ClassId</code> and tried to reach it in CS file using <code>FindByName</code>.</p>
  
  <p>This is in a Xamarin Form project running in iOS Emulator.</p>
  
  <p>In <code>MainPage.XAML</code> file,</p>
  
  <pre><code>&lt;StackLayout&gt;
      &lt;!-- Place new controls here --&gt;
      &lt;Button Text="" /&gt;
      &lt;Label ClassId="myLabel1"
         Text="Xamarin Form Demo" 
         HorizontalOptions="Center"/&gt;
      &lt;Button ClassId ="btnNum"
         Text="Image Control" 
         HorizontalOptions="Center" /&gt;
      &lt;Image ClassId="myImg" 
         HorizontalOptions="Center" /&gt;
      &lt;Label ClassId="myLabel2"
         Text="Number" 
         HorizontalOptions="Center"/&gt;
  &lt;/StackLayout&gt;
  </code></pre>
  
  <p>In MainPage.xaml.cs file,</p>
  
  <pre><code>    public MainPage()
      {
          InitializeComponent();
  
          int num = 0;
          ((Button)FindByName("btnNum")).Clicked += (o, e) ((Label)FindByName("myLabel2")).Text = (++num).ToString();
      }
  </code></pre>
  
  <p>When I run the app on iOS Emulator, I always get the following error in the longest line of code in <code>MainPage.xaml.cs</code> file,</p>
  
  <blockquote>
    <p>*********<br>
    Unhandled Exception:</p>
    
    <p>System.NullReferenceException: Object reference not set to an instance
    of an object</p>
    
    <p>*********</p>
  </blockquote>
  
  <p>Thanks for the help, and it would be nice if I can get some further information about accessing control from a different content page.</p>