title: Access Method from other Class in Xamarin
body: <p>I'm stuck with a Xamarin problem. I have a XAML ContentPage file which consists of two ContentView (vm:) in a StackLayout:</p>
  
  <pre><code>&lt;ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              xmlns:local="clr-namespace:Proj1"
              xmlns:vm="clr-namespace:Proj1.ViewModels"
              x:Class="Proj1.MyMain"&gt;
  
      &lt;StackLayout BackgroundColor="{StaticResource MainBG}" Spacing="1"&gt;
          &lt;vm:DisplayArea /&gt;
          &lt;vm:ButtonArea /&gt;
      &lt;/StackLayout&gt; 
  &lt;/ContentPage&gt;
  </code></pre>
  
  <p>The two vm: presents two ContentView areas for labels and buttons. I separated these for simplicity and to keep the XAML files smaller.</p>
  
  <p>So, the general, <em>merged</em> XAML structure looks like this:</p>
  
  <pre><code>&lt;ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              xmlns:local="clr-namespace:Proj1"
              xmlns:vm="clr-namespace:Proj1.ViewModels"
              x:Class="Proj1.MyMain"&gt;
  
      &lt;StackLayout BackgroundColor="{StaticResource MainBG}" Spacing="1"&gt;
          &lt;ContentView&gt;
              ...
              &lt;Label Grid.Row="0" Grid.Column="1" x:Name="InpRegX" /&gt;
              ...
          &lt;/ContentView&gt;
  
          &lt;ContentView&gt;
              ...
              &lt;Button ... Clicked="BtnClicked" /&gt;
              ...
          &lt;/ContentView&gt;
      &lt;/StackLayout&gt; 
  &lt;/ContentPage&gt;
  </code></pre>
  
  <p>But I want to have the two ContentView in separate files.</p>
  
  <p>DisplayArea consists among others of a label RegX:</p>
  
  <pre><code>&lt;ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              x:Class="Proj1.ViewModels.DisplayArea"&gt;
      ...
      &lt;Label Grid.Row="0" Grid.Column="1" x:Name="InpRegX" /&gt;
      ...
  &lt;/ContentView&gt;
  
  
  namespace Proj1.ViewModels
  {
      public partial class DisplayArea : ContentView
      {
          public readonly MyClass RegX;  // made public for simplicity
  
          public DisplayArea ()
          {
              InitializeComponent ();
  
              RegX = new MyClass(InpRegX);
          }
      }
  }
  </code></pre>
  
  <p>Now I want to execute a method .AddChar() of DisplayArea.RegX from a button clock.</p>
  
  <pre><code>namespace Proj1.ViewModels
  {
      public partial class ButtonArea : ContentView
      {
          public ButtonArea ()
          {
              InitializeComponent ();
          }
  
          private void BtnClicked(object sender, EventArgs e)
          {
              var btn = (Button)sender;
  
              DisplayArea.RegX.AddChar(btn.Text);  // ERROR!
          }
      }
  }
  </code></pre>
  
  <p>This creates a compiler error:</p>
  
  <blockquote>
    <p>An object reference is required for the non-static field, method, or property 'DisplayArea.RegX</p>
  </blockquote>
  
  <p>This is because I reference RegX via its <em>class</em>, not the real <em>object instance</em>. But how can I find the name the compiler creates for the instance?</p>