android - 带有 Mvvmcross 的 Xamarin 表单选项卡式页面

标签 android xamarin visual-studio-2015 xamarin.forms mvvmcross

我遇到了一个奇怪的问题,当我将内容页面设置为启动页面时,Xamarin Forms App 工作正常。如果我将 TabbedPage 设置为启动项并将相同的 ContentPage 设置为 TabbedPage 的子项,则它不会显示/数据绑定(bind) ContentPage。没有错误。我错过了什么想法?这是我的 TabbedPage View 模型。

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>

最佳答案

经过大量试验和错误以及社区的帮助,这是有效的方法。

将 BindingContext 设置为 ContentPage 代码隐藏 C#,如下所示:

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

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

public class HomeViewModel : MvxViewModel

    {
        private readonly IMobileService service;

        public HomeViewModel(IMobileService service)
        {
            this.service = service;
            //Content = service.GetContent; //Get your data
        }
     }

关于android - 带有 Mvvmcross 的 Xamarin 表单选项卡式页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40187469/

相关文章:

c# - 生成警告 _GetPrimaryCpuAbi 目标 -> Xamarin.Android.Common.Debugging.targets(311,2) : warning : One or more errors occurred

exception-handling - Visual Studio 2015 wdk 10 命令行错误

c# - 如何将字节数组转换为 FileImageSource?

Android MVVM Cross v3 - 如何创建具有交替行颜色的 ListView

android - 创建一个 DateTimePicker React-Native Android?

java - 如何以编程方式退出 Android 应用程序?

c++ - Visual Studio : project is not up to date "because "AlwaysCreate"was specified"?

sql-server - SQL71501 - 如何抑制此错误

java - 在同一轴上平移和旋转时出现奇怪的行为

android - 房间改造 Dagger2 MVP : Error: cannot find symbol variable DaggerAppComponent