【问题标题】:How to Display New Item on MvxListView on Button Click in Android Xamarin Visual Studio如何在 Android Xamarin Visual Studio 中单击按钮时在 MvxListView 中显示新项目
【发布时间】:2017-01-11 17:45:55
【问题描述】:

我想在每次单击按钮时显示新的 TextView 行。我正在使用 MvvmCross,我知道我应该使用分配给布局模板的 MvxListView,但我不知道如何在单击按钮时将其显示在屏幕上。

谁能给我一个从 ViewModel 执行此操作的简单代码示例,以及关于启动新 TextView 显示的哪一行代码的简短说明?

更新

我在下面提供我的代码:

ViewModel.cs

public class FirstViewModel 
    : MvxViewModel
{
    private ObservableCollection<Unit> unitCodes;
    public ObservableCollection<Unit> UnitCodes
    {
        get { return unitCodes; }
        set { unitCodes = value; RaisePropertyChanged(() => UnitCodes); }
    }

    public IMvxCommand ButtonCommand { get; private set; }
    public FirstViewModel()
    {
        unitCodes = new ObservableCollection<Unit>();
        ButtonCommand = new MvxCommand(() =>
        {
            UnitCodes = UnitCodes ?? new ObservableCollection<Unit>();
            UnitCodes.Add(new Unit("123", "Test Name"));
        });
    }

    public class Unit : MvxViewModel
    {
        private string unitCode;
        public string UnitCode
        {
            get { return unitCode; }
            set { unitCode = value; RaisePropertyChanged(() => UnitCode); }
        }
        private string unitName;
        public string UnitName
        {
            get { return unitName; }
            set { unitName = value; RaisePropertyChanged(() => UnitName); }
        }

        public Unit() { }
        public Unit(string unitCode, string unitName)
        {
            UnitCode = unitCode;
            UnitName = unitName;
        }
    }
}

View.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:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="40dp"
        local:MvxBind="Click ButtonCommand"
        android:text="Click To Add" />
    <Mvx.MvxListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        local:MvxItemTemplate="@layout/unitcodeitemlayout"
        local:MvxBind="ItemSource UnitCodes" />
</LinearLayout>

unitcodeitemlayout.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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        local:MvxBind="Text UnitName" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        local:MvxBind="Text UnitCode" />
</LinearLayout>

我的代码没有任何编译或运行时错误,但是单击按钮时没有发生任何事情。我想要的是每次单击按钮时将“123”和“测试名称”添加到显示中。

【问题讨论】:

    标签: android mvvm xamarin mvvmcross mvxlistview


    【解决方案1】:

    您的代码是正确的。但是,您在MvxListView axml 下拼错了ItemsSource

    应该是:

    <Mvx.MvxListView
         ...
         local:MvxBind = "ItemsSource UnitCodes"/>
    

    【讨论】:

    • 你找到了!谢谢! +1 彻底检查我的代码。
    【解决方案2】:

    为了设置ListView,看看这个MvvmCross tutorial

    你可能会得到这样的结果:

    视图模型:

    public class MyListViewModel : MvxViewModel
        {
            private ObservableCollection<TextViewCellViewModel> _listItems;
    
            public virtual ObservableCollection<TextViewCellViewModel> ListItems {
                get {
                    return _listItems;
                }
                set {
                    _listItems = value;
                    RaisePropertyChanged(() => ListItems);
                }
            }
    
            private IMvxCommand _addTextViewCommand;
    
            public IMvxCommand AddTextViewCommand {
                get {
                    _addTextViewCommand = _addTextViewCommand ?? new MvxCommand(AddTextView);
                    return _addTextViewCommand;
                }
            }
    
    
            private void AddTextView()
            {
                ListItems = ListItems ?? new ObservableCollection<TextViewCellViewModel>();
    
                ListItems.Add(new TextViewCellViewModel());
            }
    
    
            public class TextViewCellViewModel : MvxViewModel
            {
                private string _text;
                public string Text { get { return _text; } set { _text = value; RaisePropertyChanged(() => Text); } }
    
                public TextViewCellViewModel() { }
            }
        }
    

    Android ListView 布局:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:local="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="fill_vertical"
        android:paddingRight="20dp"
        android:paddingLeft="20dp">
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/frameLayout"
            android:paddingBottom="50dp">
            <MvxListView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scrollbars="vertical"
                android:id="@+id/list_view_tripplanner_result"
                android:divider="@android:color/transparent"
                android:paddingTop="10dp"
                android:clipToPadding="false"
                android:dividerHeight="7dp"
                android:descendantFocusability="afterDescendants"
                local:MvxItemTemplate="@layout/cell_textview"
                local:MvxBind="ItemsSource ListItems;" />
        </FrameLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:paddingTop="5dp"
            android:paddingBottom="10dp">
            <Button
                android:layout_width="match_parent"
                android:layout_height="40dp"
                local:MvxBind="Click AddTextViewCommand"
                android:text="Add TextView"
                android:textColor="@color/primary_white"
                android:background="@android:color/darker_gray" />
        </LinearLayout>
    </RelativeLayout>
    

    应该如下所示:(Listview 底部的通知按钮)

    @layout/cell_textview 看起来像这样:

    ListView 的 ItemTemplate 布局为:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:local="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:textSize="20dp"
            local:MvxBind="Text Text"/>
    </LinearLayout>
    

    【讨论】:

    • 您好,感谢您的回复。我试过了,但我仍然遇到同样的问题:单击按钮时没有任何反应。我会上传我的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    相关资源
    最近更新 更多