在 MvxListView 中绑定 ItemTemplate

Binding ItemTemplate in MvxListView

我正在使用 Mvvmcross v.6.1.2 开发简单的 Android 应用程序,但在使用 MvxListView ItemTemplate 绑定时遇到了问题。 这是我的 ListView,它已正确绑定到我的 ViewModel 中的 MvxObservableCollection Weather:

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:local="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
            <MvxListView
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:divider="#000000"
                android:dividerHeight="3px"
                local:MvxBind="ItemsSource Weather; ItemClick ListItemClickCommand"
                local:MvxItemTemplate="ItemTemplateId weatherListItem"
                SelectedItem="{mvx:MvxBind CurrentWeather}"/>
    </LinearLayout>

这是我的 ListView ItemTemplate:

<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"
              android:background="#ffffff">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="2dp"
        android:layout_gravity="center_horizontal"
        local:MvxBind="Text WeatherName"/>
</LinearLayout>

local:MvxItemTemplate="ItemTemplateId weatherListItem" 有什么问题吗?

您的 ItemTemplate 布局名为 "weatherListItem" 吗?如果是这样,您需要这样指定 ItemTemplate 和 SelectedItem:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <MvxListView
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:divider="#000000"
        android:dividerHeight="3px"
        local:MvxBind="ItemsSource Weather; SelectedItem CurrentWeather; ItemClick ListItemClickCommand"
        local:MvxItemTemplate="@layout/weatherListItem"/>
</LinearLayout>

此处提供一些示例代码: https://github.com/MvvmCross/MvvmCross-Samples/tree/master/WorkingWithCollections

您的布局有几个问题:

<MvxListView
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:divider="#000000"
    android:dividerHeight="3px"
    local:MvxBind="ItemsSource Weather; ItemClick ListItemClickCommand"
    local:MvxItemTemplate="ItemTemplateId weatherListItem"
    SelectedItem="{mvx:MvxBind CurrentWeather}"/>

第一期是

local:MvxItemTemplate="ItemTemplateId weatherListItem"

MvxItemTemplateaxml/xml 文件中使用时,它指的是用作项目模板的布局。这将是您在 android 资源中的布局。为了将它指向您的资源布局之一,您需要按如下方式编写它:

local:MvxItemTemplate="@layout/nameoflayout"

这会为 ListView 提供一个与您的布局相匹配的 ID。

这里的另一个问题是您将 Xamarin.Forms XAML 属性与 Android 属性混合:

SelectedItem="{mvx:MvxBind CurrentWeather}"

此行不会膨胀,因为 MvxListView 中没有名为 SelectedItem 的属性。如果你想绑定public属性,需要在MvxBind.

中完成
local:MvxBind="ItemsSource Weather; ItemClick ListItemClickCommand; SelectedItem CurrentWeather"