【问题标题】:What property do you use to bind data to axml layout in MvvmLight?您使用什么属性将数据绑定到 Mvvm Light 中的 xml 布局?
【发布时间】:2019-09-27 11:00:29
【问题描述】:

我整整两天都在试图弄清楚如何将数据和命令绑定到按钮或 listView 等布局元素,直到现在我还没有成功,这就是我所拥有的示例之一我的布局。

你能帮帮我吗?

<?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"
    android:minWidth="25px"
    android:minHeight="25px">
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/srlStores"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="80"
        local:MvxBind="Refreshing IsBusy">
        <MvxListView
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/lvStores"
            local:MvxBind="ItemsSource Stores; ItemClick OpenDetailCommand"
            local:MvxItemTemplate="@layout/store_list_item" />
    </android.support.v4.widget.SwipeRefreshLayout>
    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="20"
        android:id="@+id/progressBar1"
        local:MvxBind="Visible IsBusy" />
    <Button
        android:text="@string/storeListCreateButtonText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bCreate"
        android:layout_weight="20"
        local:MvxBind="Click CreateCommand" />
</LinearLayout>

【问题讨论】:

    标签: xamarin data-binding xamarin.android mvvm-light


    【解决方案1】:

    欢迎来到堆栈溢出!你非常接近。您必须确保 ViewModel 设置正确,然后只需进入与此 Layout 所属的 Fragment 关联的 ViewModel 并为按钮添加以下内容:

    private IMvxCommand _createCommand;
    public IMvxCommand CreateCommand
    {
        get
        {
            return createCommand ?? (createCommand = new MvxCommand(() =>
                {
                    // Do Some Work
                }));
        }
    }
    

    对于您的列表,您需要像这样创建一个 ObservableCollection:

    private ObservableCollection<StoreListModelWrapper> _stores;
    public ObservableCollection<StoreListModelWrapper> Stores
    {
        get { return _stores; }
        set { SetProperty(ref _stores, value); }
    }
    

    将使用点击命令调用

    public IMvxCommand<StoreListModelWrapper> _itemClickCommand;
    public IMvxCommand<StoreListModelWrapper> ItemClickCommand
    {
        get
        {
            return _itemClickCommand ?? (_itemClickCommand = new MvxCommand<StoreListModelWrapper>((item) =>  // Do Work with item.
                                       ));
        }
    }
    

    你在关注什么文档?

    【讨论】:

      猜你喜欢
      • 2019-04-28
      • 2016-04-26
      • 1970-01-01
      • 2019-01-18
      • 2016-09-26
      • 2020-08-11
      • 2014-10-06
      • 1970-01-01
      • 2023-03-15
      相关资源
      最近更新 更多