Search code examples
androidxamarinvisual-studio-2015xamarin.formsmvvmcross

Xamarin Form Tabbed Page with Mvvmcross


I am having a weird issue, Xamarin Forms App works fine when I setup Content page as a startup page. If I set TabbedPage as a startup and same ContentPage as a Children of a TabbedPage then it doesn't display/data-bind ContentPage. No errors. What am I missing any idea? Here is my TabbedPage view model.

using MvvmCross.Core.ViewModels;
using System.Windows.Input;

namespace Company.Mobile.ViewModels
{
    public class TabbedMainViewModel
        : MvxViewModel
    {

    }
}

XAML:

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:forms="using:Xamarin.Forms"         
             xmlns:local="clr-namespace:company.Mobile.Pages;assembly=company.Mobile"   
             x:Class="company.Mobile.Pages.TabbedMainPage"
            Title="Title">
  <TabbedPage.Children>
    <local:HomePage/>
    <local:MainPage/>
    <local:ResourcesPage/>
    <local:ContactPage/>    
  </TabbedPage.Children>
</TabbedPage>

Solution

  • After a lot of trial and error and help from the community, here is what worked.

    Set BindingContext to the ContentPage code-behind C#, something like below:

        public partial class HomePage : ContentPage
        {
            public HomePage()
            {
                InitializeComponent();
                var svc = Mvx.Resolve<IMobileService>();
                BindingContext = new HomeViewModel(svc);
            }    
        }
    

    Get your data in HomeViewModel constructor something like below:

    public class HomeViewModel : MvxViewModel
    
        {
            private readonly IMobileService service;
    
            public HomeViewModel(IMobileService service)
            {
                this.service = service;
                //Content = service.GetContent; //Get your data
            }
         }