title: Dynamically adding buttons to Application Bar in Windows Phone 8 & MVVM
body: <p>I have a ViewModel in my application</p>
  
  <pre><code>/// &lt;summary&gt;
  /// The sample view model
  /// &lt;/summary&gt;
  public class SampleViewModel: ViewModelBase
  {
      public SampleViewModel(IApplicationBarService applicationBarService)
      {
        this.applicationBarService = applicationBarService
        this.ApplicationBarService.AddButton("Add Bookmark", new Uri("Images/favs-toadd.png", UriKind.Relative), this.OnAddPnrBookmark);
      }
  
      /// &lt;summary&gt;
      /// Gets or sets the application bar service.
      /// &lt;/summary&gt;
      public IApplicationBarService ApplicationBarService { get; set; }   }
  </code></pre>
  
  <p>The implementation of IApplicationBarService looks like this</p>
  
  <pre><code>/// &lt;summary&gt;
  /// The application service.
  /// &lt;/summary&gt;
  public class ApplicationBarService : IApplicationBarService
  {
      /// &lt;summary&gt;
      /// Gets the application bar.
      /// &lt;/summary&gt;
      public IApplicationBar ApplicationBar
      {
          get
          {
              var currentPage = ((PhoneApplicationFrame)Application.Current.RootVisual).Content as MainPage;
              if (currentPage != null &amp;&amp; currentPage.ApplicationBar == null)
              {
                  currentPage.ApplicationBar = new ApplicationBar();
              }
  
              if (currentPage != null)
              {
                  return currentPage.ApplicationBar;
              }
  
              return null;
          }
      }
  
      /// &lt;summary&gt;
      /// The add button.
      /// &lt;/summary&gt;
      /// &lt;param name="title"&gt;
      /// The title.
      /// &lt;/param&gt;
      /// &lt;param name="imageUrl"&gt;
      /// The image url.
      /// &lt;/param&gt;
      /// &lt;param name="onClick"&gt;
      /// The on click.
      /// &lt;/param&gt;
      public void AddButton(string title, Uri imageUrl, Action onClick)
      {
          var newButton = new ApplicationBarIconButton()
          {
              Text = title,
              IconUri = imageUrl,
          };
          newButton.Click += (sender, e) =&gt; onClick.Invoke();
          this.ApplicationBar.Buttons.Add(newButton);
      }
  </code></pre>
  
  <p>}</p>
  
  <p>I am injecting the application bar into the view model and the sample view model resides in a portable library while the application bar service implementation is native to phone.</p>
  
  <p>When i run the app the code fails at </p>
  
  <p>var currentPage = ((PhoneApplicationFrame)Application.Current.RootVisual).Content as MainPage;</p>
  
  <p>as RootVisual is null and hence I am unable add buttons via
  this.ApplicationBarService.AddMethod</p>
  
  <p>My guess is this has to do something with the Page not being loaded as the same code works if i intialize the application Bar using a RelayCommand bound to a button on the page.</p>
  
  <p>I have seen some other threads(<a href="https://stackoverflow.com/questions/17700362/windows-phone-8-application-bar-button-long-tap-event">Windows Phone 8 Application Bar Button Long Tap Event</a>)
   where the PhoneApplicationPage's ApplicationBar property is bound to the ViewModel, but i cannot implement that as my view model resides in the portable class library.</p>
  
  <p>Any help appreciated.</p>