【问题标题】:Xamarin Form Tabbed Page with Mvvmcross带有 Mvvmcross 的 Xamarin 表单选项卡式页面
【发布时间】:2017-03-04 09:41:40
【问题描述】:

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

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>

【问题讨论】:

  • 嗨,我知道它很久以前了。但是,您是否遇到过在 MvvmCross xamarin 表单中使用选项卡式页面的任何解决方案。

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


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    我想说,您可以通过为 XAML 中的每个选项卡式页面添加此内联属性来更轻松地做到这一点,例如主页应该是BindingContext="{Binding HomePageViewModel}"

    【讨论】:

      猜你喜欢
      • 2020-05-27
      • 2017-10-11
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-25
      相关资源
      最近更新 更多