2017-03-17 2 views
0

Ich habe MvxListView in Android erstellt. Ich verbinde es mit ObservableCollection.MvxListView - Schaltfläche in der Vorlage

Alles funktioniert gut. Selbst SelectedItem Command wird korrekt ausgelöst. Das Problem ist, wenn ich Knopf innerhalb jedes Artikels hinzufüge.

Meine Liste enthält Bilder mit Schaltflächen zum Entfernen voneinander.

Es gibt 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> 

Das ist wirklich stranbe weil Foto und Kommentar Eigenschaften sind verbindlich.

public MvxCommand RemoveCommand { get; set; } 

Allerdings entfernt RemoveCommand nicht. Irgendwelche Ideen?

EDIT:

Wenn ich hinzufügen Befehl zu meinem Modell seiner Arbeits das ist, warum Eigenschaften sind verbindlich. Wie kann ich dann in ViewModel not Model Events erstellen?

In XAML konnte ich Vorfahren Datenkontext binden ist es dort möglich?

Antwort

0

Leider ist das eine Einschränkung von MvvmCross. Die MvxListView unterstützt keine Bindung an ihr übergeordnetes ViewModel.

Ich habe sogar eine mögliche Lösung gepostet, die das Messenger-Plugin verwendet, um mit der übergeordneten VM zu kommunizieren. Bind button click inside customlayout using mvvmcross and mvxlistview

Allerdings habe ich in letzter Zeit einfach ein Wrapper ViewModel für meine Listview-Elemente erstellt und meine ganze Logik dort hineingelegt. Wenn ich Zugriff auf das übergeordnete Element benötige, füge ich einfach eine Eigenschaft dafür in meine Wrapper-VM ein.

Etwas wie:

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

Ich habe für dieses Problem benutzerdefinierten Wrapper erstellt:

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

So Ansichtsmodell implementiert ICommandableNestedCollection Schnittstelle, wo T Artikel ist

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