【问题标题】:MvvmCross MvxListView Items Not Rendering on AndroidMvvmCross MvxListView 项目未在 Android 上呈现
【发布时间】:2019-03-09 15:06:39
【问题描述】:

在将 MvxListView 绑定到我的 ViewModel 时,我似乎遇到了一个奇怪的问题。我已经对 ViewModel 进行了三次检查,并且可以确认它连接正确,因为其他两种原始类型绑定正确。

但是,当我尝试绑定到 ObservableCollection(或 MvxObservableCollection)时,在 MvxListView 中没有呈现任何项目。我跟踪了调试日志,看不到任何 MvvmCross 错误,也看不到任何 Android 膨胀问题。

我已经尝试了各种提高 propertychanged 的​​方法,正如您将在我的代码中看到的那样。

这快把我逼疯了!任何帮助将不胜感激。也许我错过了一些愚蠢的东西。

视图模型:

private MvxObservableCollection<ReminderDto> _reminders;

    public MainViewModel(IApiService apiService)
    {
        _apiService = apiService;
    }

    public override void ViewAppearing()
    {
        Reminders = new MvxObservableCollection<ReminderDto>()
        {
            new ReminderDto() { Description = "Test" },
            new ReminderDto() { Description = "Test" },
            new ReminderDto() { Description = "Test" },
            new ReminderDto() { Description = "Test" },
            new ReminderDto() { Description = "Test" },
            new ReminderDto() { Description = "Test" },
            new ReminderDto() { Description = "Test" },
        };

        RaisePropertyChanged(nameof(Reminders));
    }

    public MvxObservableCollection<ReminderDto> Reminders
    {
        get { return _reminders; }
        set { SetProperty(ref _reminders, value); }
    }

MainView.axml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<MvxListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            local:MvxBind="ItemSource Reminders"
            local:MvxItemTemplate="@layout/reminder_row" />

项目模板 (reminder_row.axml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/reminder_danger"
android:weightSum="6">
<TextView
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minWidth="25px"
    android:minHeight="25px"
    android:id="@+id/textView1" />

【问题讨论】:

    标签: android xamarin mvvmcross mvxlistview


    【解决方案1】:

    加载集合的属性是ItemsSource,而不是ItemSource。 替换这个:

    local:MvxBind="ItemSource Reminders"
    

    有了这个:

    local:MvxBind="ItemsSource Reminders"
    

    【讨论】:

    • 很高兴它有帮助。编码愉快!
    • 通常您可以在应用程序输出中发现这些问题。它会抱怨绑定表达式错误,并告诉你是源还是目标错误。在您的情况下,它可能会说找不到目标ItemSource
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    相关资源
    最近更新 更多