title: WP7 Access MainPage Elements
body: <p>I'm trying to make a simple Windows Phone 7 Silverlight app. There's the <code>MainPage.xaml</code> with some UI elements, and a separate C# class <code>MyClass.cs</code> with some code.</p>
  
  <p><strong>My problem is</strong>: <code>MyClass</code> can't access anything contained in <code>MainPage</code> (i.e. it doesn't know the UI elements or C# methods exist).</p>
  
  <p>If I try to inherit <code>MainPage</code>, the app compiles, but refuses to run:</p>
  
  <pre><code>public class MyClass : MainPage
  {
     // No good
  }
  </code></pre>
  
  <p>If I try <a href="https://stackoverflow.com/questions/1728879/silverlight-3-0-how-to-access-a-mainpage-control-value-from-an-usercontrol">this solution</a>, then I get an <code>InvalidCastException</code> error:</p>
  
  <pre><code>public class MyClass
  {
    // Also no good
    MainPage m = (MainPage)Application.Current.RootVisual;
  }
  </code></pre>
  
  <p><strong>My question is</strong>: how can I access <code>MainPage</code> from a separate class? </p>
  
  <p>In <code>MainPage.xaml.cs</code>, I can simply use <code>myElement.Property</code> etc. However, this isn't possible in <code>MyClass</code>, but I'm not sure why. </p>
  
  <p>I guess there's a simple answer that I'm missing, but I'm really not sure what it is (... C# newbie trying to run before he can walk!?).</p>
  
  <p>Thanks in advance for any advice you can offer.</p>