MvvmCross数据绑定未按预期工作

时间:2016-11-17 09:50:09

标签: android xamarin.android mvvmcross

我目前在Android上遇到MvvmCross的奇怪问题。我想将数据绑定到ViewModel。我的设置如下

public class ForumTopicItemsViewModel : MvxViewModel
{
    ObservableCollection<ForumReplyListModel> _forumComments;
    ForumTopicDetailsModel _topic;
    long _forumId;
    long _topicId;
    bool _busy;
    int _page;

    public ForumTopicItemsViewModel()
    {
        _topic = new ForumTopicDetailsModel()
        {
            DateCreated = DateTime.Now.AddDays(-1),
            Discussion = "Just the fact that I dont care about what is displayed here",
            ForumId = ForumId,
            TopicId = TopicId,
            Topic = "Another Heading, Just for the sake of it"
        };
        _forumComments = new ObservableCollection<ForumReplyListModel>
        {
            new ForumReplyListModel()
            {
                CreatedBy = "Peter Edike",
                DateCreated = DateTime.Now.AddDays(-1),
                Reply = "Just a reply to show how far I have come"
            }
        };
    }

    public int Page
    {
        get { return _page; }
        set { SetProperty(ref _page, value); }
    }

    private static bool IsHostReachable()
    {
        var nReachability = Mvx.Resolve<IMvxReachability>();
        return nReachability.IsHostReachable(AppConfiguration.RequestUrl);
    }

    public ObservableCollection<ForumReplyListModel> Comments
    {
        get { return _forumComments; }
        set { SetProperty(ref _forumComments, value); }
    }

    public ForumTopicDetailsModel  Topic
    {
        get { return _topic; }
        set { SetProperty(ref _topic, value); }
    }

    public string Heading => this.Topic.Topic;
    public string Description => this.Topic.Discussion;
    public string DateCreatedString => this.Topic.DateCreatedString;

    public bool Busy
    {
        get { return _busy; }
        set { SetProperty(ref _busy, value); }
    }

    public long ForumId
    {
        get { return _forumId; }
        set {SetProperty(ref _forumId, value); }
    }

    public long TopicId
    {
        get { return _topicId; }
        set { SetProperty(ref _topicId, value); }
    }

    public void Init(long TopicId, long ForumId)
    {
        this.TopicId = TopicId;
        this.ForumId = ForumId;
    }
}

我有Android XML View Like So

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent">
            <MvvmCross.Droid.Support.V4.MvxSwipeRefreshLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:MvxBind="Refreshing Busy; RefreshCommand LoadReplyCommand">
                <MvxRecyclerView
                    android:scrollbars="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:MvxItemTemplate="@layout/listitem_commentlist"
                    app:MvxBind="ItemsSource Comments;" />
            </MvvmCross.Droid.Support.V4.MvxSwipeRefreshLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="4px"
                android:background="@color/grey_soft" />
        </LinearLayout>
        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent">
            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                app:cardCornerRadius="@dimen/spacing_medium"
                app:cardElevation="@dimen/spacing_xsmall"
                app:cardUseCompatPadding="true">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="@dimen/spacing_medium"
                        android:layout_marginLeft="@dimen/spacing_large"
                        android:layout_marginRight="@dimen/spacing_large"
                        android:layout_marginTop="@dimen/spacing_large"
                        android:textAppearance="@style/TextAppearance.AppCompat.Headline"
                        android:textColor="@color/upload_photo_button_color"
                        app:MvxBind="Text Heading" />
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/spacing_large"
                        android:text="@string/lorem_ipsum"
                        app:MvxBind="Text Description"
                        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                        android:textColor="@color/grey_dark" />
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="2px"
                        android:background="@color/grey_soft" />
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/spacing_large"
                        app:MvxBind="Text DateCreatedString"
                        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                        android:textColor="@color/upload_photo_button_color"
                        android:textStyle="italic" />
                </LinearLayout>
            </android.support.v7.widget.CardView>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

问题:

即使我确保集合在构造函数中有项目,也永远不会显示MvxRecyclerView的内容。

其他所有绑定都按预期工作。但是,如果我注释掉MvxRecyclerView以外的所有内容,它就会变得可见。

我想我做错了什么。

有人能指出我正确的方向吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

您的所有LinearLayout似乎都没有方向设置。我认为默认设置为Horizontal,这意味着您的布局从左向右流动并超出屏幕的可见区域。

另一个问题是您的布局非常嵌套,这意味着当它将在屏幕上绘制时,它可能会有大量的透支和额外的测量过程。您可以使用单个RelativeLayout代替LinearLayout的4个嵌套图层来取消嵌套。

这样的事可能有用:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <MvvmCross.Droid.Support.V4.MvxSwipeRefreshLayout
        android:layout_alignParentTop="true"
        android:layout_above="@+id/divider"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:MvxBind="Refreshing Busy; RefreshCommand LoadReplyCommand">
        <MvxRecyclerView
            android:scrollbars="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:MvxItemTemplate="@layout/listitem_commentlist"
            app:MvxBind="ItemsSource Comments" />
    </MvvmCross.Droid.Support.V4.MvxSwipeRefreshLayout>
    <View
        android:id="@+id/divider"
        android:layout_above="@+id/heading_card"
        android:layout_width="match_parent"
        android:layout_height="4px"
        android:background="@color/grey_soft" />
    <android.support.v7.widget.CardView
        android:id="@+id/heading_card"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        app:cardCornerRadius="@dimen/spacing_medium"
        app:cardElevation="@dimen/spacing_xsmall"
        app:cardUseCompatPadding="true">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/spacing_medium"
                android:layout_marginLeft="@dimen/spacing_large"
                android:layout_marginRight="@dimen/spacing_large"
                android:layout_marginTop="@dimen/spacing_large"
                android:textAppearance="@style/TextAppearance.AppCompat.Headline"
                android:textColor="@color/upload_photo_button_color"
                app:MvxBind="Text Heading" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/spacing_large"
                android:text="@string/lorem_ipsum"
                app:MvxBind="Text Description"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                android:textColor="@color/grey_dark" />
            <View
                android:layout_width="match_parent"
                android:layout_height="2px"
                android:background="@color/grey_soft" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/spacing_large"
                app:MvxBind="Text DateCreatedString"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                android:textColor="@color/upload_photo_button_color"
                android:textStyle="italic" />
        </LinearLayout>
    </android.support.v7.widget.CardView>
</RelativeLayout>