与ViewModel的ViewPager绑定问题

时间:2015-12-11 08:38:45

标签: android binding android-viewpager mvvmcross

我正在使用ViewPager在MvvmCross 4.0.0-beta5中实现标签。但是,ViewModel的EditText绑定属性始终为null。我也尝试更新到MvvmCross 4.0.0-beta7,但没有运气。

这是我的ViewPager

 <android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <include
        android:id="@+id/mspManagerToolbar"
        layout="@layout/mspmanagertoolbar" />
    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="16dp"
        local:tabGravity="fill"
        local:tabMode="scrollable" />
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        local:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.AppBarLayout>

这是我的标签视图

<EditText
    ...
    local:MvxBind="Text Title" />

上面的EditText绑定不起作用。标题总是为空。

<EditText
    ...
    local:MvxBind="Text User.Title" />

以上EditText绑定工作完美。 User.Title获得正确的值。

这里是ViewPagerView.cs代码

public class CustomerDetailView : MvxCachingFragmentCompatActivity<CustomerDetailViewModel>
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.CustomerDetailView);

        // Use toolbar as action bar
        var toolbar = FindViewById<Toolbar>(Resource.Id.mspManagerToolbar);

        if (toolbar != null)
        {
            // Toolbar will now take on default actionbar characteristics
            SetSupportActionBar(toolbar);

        }

        // View Pager
        var viewPager = FindViewById<ViewPager>(Resource.Id.viewpager);
        if (viewPager != null)
        {
            // Add tabs in view pager
            var fragments = new List<MvxFragmentStatePagerAdapter.FragmentInfo>
            {
                new MvxFragmentStatePagerAdapter.FragmentInfo(TAB_DETAIL, typeof(Tab_CustomerDetail), typeof(CustomerDetailViewModel)),
                new MvxFragmentStatePagerAdapter.FragmentInfo(TAB_CONTACT, typeof(Tab_CustomerContact), typeof(CustomerDetailViewModel)),
                new MvxFragmentStatePagerAdapter.FragmentInfo(TAB_LOCATION, typeof(Tab_CustomerLocation), typeof(CustomerDetailViewModel)),
                new MvxFragmentStatePagerAdapter.FragmentInfo(TAB_CONTRACT, typeof(Tab_CustomerContract), typeof(CustomerDetailViewModel))                   
            };
            viewPager.Adapter = new MvxFragmentStatePagerAdapter(this, SupportFragmentManager, fragments);
        }

        var tabLayout = FindViewById<TabLayout>(Resource.Id.tabs);
        tabLayout.SetupWithViewPager(viewPager);
    }
}

这是我的片段代码

[Register("app.droid.views.fragments.Tab_CustomerDetail")]
public class Tab_CustomerDetail : MvxFragment
{        

    #region Public Method

    /// <summary>
    /// tab customer detail view
    /// </summary>
    /// <param name="inflater"></param>
    /// <param name="container"></param>
    /// <param name="savedInstanceState"></param>
    /// <returns></returns>
    public override View OnCreateView(Android.Views.LayoutInflater inflater, Android.Views.ViewGroup container, Android.OS.Bundle savedInstanceState)
    {
        var ignored = base.OnCreateView(inflater, container, savedInstanceState);

        var view = this.BindingInflate(Resource.Layout.frg_tab_CustomerDetail, null);


        return view;
    }

    #endregion

}

这是ViewModel属性

public string Title
{
    get
    {
        return (title);
    }
    set
    {
        if (value != title)
        {
            title = value;

            RaisePropertyChanged(() => this.Title);
        }
    }
}

public User User
{
    get
    {
        return (repositoryService.User);
    }
    set
    {
        repositoryService.User = value;
        RaisePropertyChanged(() => User);
    }
}

标题属性变为null,User.Title工作正常。那么请告诉我哪里出错了?

2 个答案:

答案 0 :(得分:0)

更改属性是否修复了它?

public string Title
{
    get
    {
        return title;
    }
    set
    {
        title = value;
        RaisePropertyChanged(() => this.Title);
    }
}

答案 1 :(得分:0)

问题是我从活动中保存了我的数据,所以我没有获得视图的价值。因为对于每个片段,它都会创建视图模型的新实例,因为没有获得视图的更新值。现在我从片段中保存我的数据,现在我在视图模型中获得所有视图值。