具有Mvvmcross的Xamarin表单选项卡式页面 [英] Xamarin Form Tabbed Page with Mvvmcross

查看:231
本文介绍了具有Mvvmcross的Xamarin表单选项卡式页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个奇怪的问题,当我将内容"页面设置为启动页面时,Xamarin Forms App可以正常工作.如果我将TabbedPage设置为启动项,并将ContentPage设置为TabbedPage的子级项,则它不会显示/数据绑定ContentPage.没有错误.我想念什么?这是我的TabbedPage视图模型.

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>

推荐答案

经过社区的反复试验和帮助,这才是有效的方法.

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

将BindingContext设置为C#背后的ContentPage代码,如下所示:

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

在HomeViewModel构造函数中获取数据,如下所示:

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

这篇关于具有Mvvmcross的Xamarin表单选项卡式页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆