【问题标题】:MvxListView - button inside templateMvxListView - 模板内的按钮
【发布时间】:2017-03-17 12:13:15
【问题描述】:

我在 Android 中创建了 MvxListView。我将它绑定到 ObservableCollection。

一切正常。甚至 SelectedItem 命令也能正确触发。问题是当我在每个项目中添加按钮时。

我的列表包含带有按钮的图像,可以相互删除。

item_photo.axml

<LinearLayout
    android:id="@+id/titleLinearLayout"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:background="@color/icons"
    android:orientation="horizontal">
  <TextView
      android:id="@+id/DepartureDateTitleTextView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      local:MvxBind="Text Comment"
      android:textColor="@color/primary"
      android:textSize="17dp" />
  <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            local:MvxBind="Bitmap NativeReference, Converter=ObjectToBitmap, FallbackValue=''"/>
</LinearLayout>
<LinearLayout
       android:id="@+id/titleLinearLayout"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:background="@color/icons"
       android:orientation="horizontal">
  <Button
      android:id="@+id/PriceTextView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Remove"
      android:textColor="@color/accent"
      android:textSize="17dp"
      local:MvxBind="Click RemoveCommand" />
</LinearLayout>

这真的很奇怪,因为 Photo 和 Comment 属性是绑定的。

public MvxCommand RemoveCommand { get; set; }

但是 RemoveCommand 不会触发。有什么想法吗?

编辑:

当我将 Command 添加到我的模型时,它的工作原理就是属性绑定的原因。那么我怎样才能在 ViewModel 而不是 Model 中实现事件呢?

在 XAML 中我可以绑定祖先的数据上下文,那里有可能吗?

【问题讨论】:

    标签: c# android xamarin mvvmcross


    【解决方案1】:

    不幸的是,这是 MvvmCross 的限制。 MvxListView 不支持绑定到其父 ViewModel。

    我什至发布了一个可能的解决方案,它使用 Messenger 插件与父 VM 进行通信。 Bind button click inside customlayout using mvvmcross and mvxlistview

    但是,最近我只是为我的列表视图项目创建了一个包装 ViewModel,并将我的所有逻辑都放在那里。如果我需要访问父级,我只需在我的包装 VM 中为其添加一个属性。

    类似:

    public class PhotoViewModel {
       public SomeViewModel ParentViewModel { get; set; }
       public PhotoModel Photo { get; set; }
       public ICommand RemovePhotoCommand {
          get {
              return new MvxCommand(() => {
                  ParentViewModel.RemovePhotoCommand.Execute(this);
              });
          }
       }
    }
    

    【讨论】:

      【解决方案2】:

      我已经为这个问题创建了自定义包装器:

         public class CommandableCollectionItem<TItem, TViewModel>
                                  where TViewModel : ICommandableNestedCollection<TItem>
          {
              public TItem Item { get; private set; }
              private readonly TViewModel _viewModel;
      
              public CommandableCollectionItem(TItem item, TViewModel viewModel)
              {
                  this.Item = item;
                  this._viewModel = viewModel;
              }
      
              public IMvxCommand Execute => new MvxCommand(() => _viewModel.OnExecute(Item));
      
          }
      
      
      public interface ICommandableNestedCollection<T>
          {
              void OnExecute<T>(T item);
          }
      

      所以 ViewModel 实现了 ICommandableNestedCollection 接口,其中 T 是 Item

      MyViewModel : ICommandableNestedCollection<TakenPhotoModel>
      {
        public void OnExecute<T>(T item)
        {
           //code
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-09
        • 2017-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-26
        • 1970-01-01
        • 2015-02-04
        相关资源
        最近更新 更多