2017-03-17 82 views
0

我已經在Android中創建了MvxListView。我將它綁定到ObservableCollection。MvxListView - 模板內的按鈕

一切工作正常。即使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> 

這是真的,因爲stranbe照片和評論性具有約束力。

public MvxCommand RemoveCommand { get; set; } 

但是RemoveCommand does not fires。有任何想法嗎?

編輯:

當我add命令我的模型及其工作這就是爲什麼特性結合。那麼我怎麼能在ViewModel不是Model中存檔事件?

在XAML中,我可以綁定祖先數據上下文嗎?

回答

0

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

我甚至發佈了一個可能的解決方案,它使用Messenger插件與父虛擬機進行通信。 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); 
      }); 
     } 
    } 
} 
0

我創建定製的包裝對於這個問題:

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); 
    } 

因此視圖模型實現ICommandableNestedCollection接口,其中T是項目

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