Xamarin Form Tabbed Page with Mvvmcross

五迷三道 提交于 2019-12-21 23:34:45

问题


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>

回答1:


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
        }
     }



回答2:


I would say that you can do that easier by adding this inline property for an each your tabbed page in XAML, e.g. for the home page it should be BindingContext="{Binding HomePageViewModel}"



来源:https://stackoverflow.com/questions/40187469/xamarin-form-tabbed-page-with-mvvmcross

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!