gpt4 book ai didi

c# - Xamarin : Get MvxRecyclerView working

转载 作者:行者123 更新时间:2023-11-29 19:12:34 26 4
gpt4 key购买 nike

我正在使用 Xamarin 和 MvvmCross 构建一个不错的 Android 应用程序。我正在尝试让 MvxRecyclerView 正常工作,但我做不对。该应用运行但未在 RecycleView 中显示任何内容

这是我创建的 View (MainView.axml):

<?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="vertical">
<MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView
android:id="@+id/rvItems"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
local:MvxItemTemplate="@layout/itemview"
local:MvxBind="ItemsSource TestItems" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add" />
</LinearLayout>

我创建的 ViewModel (MainViewModel.cs):

public class MainViewModel : MvxViewModel
{
#region Properties

public ObservableCollection<TestModel> TestItems { get; set; }

#endregion

public MainViewModel()
{
TestItems = new ObservableCollection<TestModel>
{
new TestModel()
{
Name = "Test1"
},
new TestModel()
{
Name = "Test2"
}
};
}

public override Task Initialize()
{
//TODO: Add starting logic here

return base.Initialize();
}
}

项目 View (ItemView.cs):

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://scehmas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name" />

正如您在示例中看到的,它很简单,但我无法让它工作。

Github link

最佳答案

这里有两个不同的问题:

  1. ItemTemplate 的宽度为 0dp,因此它不会呈现。将其更改为 match_parent。我看到您正在尝试改用 layout_weight,但要使其正常工作,父级 LinearLayout 需要一个 weightSum

    <
  2. 您的 TextView 未绑定(bind)到模型中 Name 的值。将 local:MvxBind="Text Name" 添加到 TextView

进行这两项更改,您应该会以预期的方式看到您的 RecyclerView:

enter image description here

关于c# - Xamarin : Get MvxRecyclerView working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44775109/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com