2016-04-29 16 views
1

In der FundamentalView.cs habe ich Click-Ereignis, das ein Fragment von der Unterseite der Ansicht mit Optionen (Hinzufügen einer neuen Person und neue Berechnungen) auslöst.DialogFragment in MVVMCross ablehnen

var addButton = view.FindViewById<ImageButton>(Resource.Id.addButton); 
addButton.Click += OnAddButtonClick; 

void OnAddButtonClick(object sender, System.EventArgs e) 
{ 
    var dialog = new CardDialogView(); 
    dialog.ViewModel = new CardDialogViewModel(); 
    dialog.Show(((MainView)Activity).SupportFragmentManager, "CardDialogView");   
} 

CardDialogView.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" 
    android:background="#FFFFFF"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:drawableLeft="@drawable/dash_add_computer" 
     android:textColor="@color/primary_text" 
     android:textSize="16sp" 
     android:text="New Calculation" 
     local:MvxBind="Click NewCalculationCommand"/> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:drawableLeft="@drawable/dash_add_head" 
     android:drawablePadding="28dp" 
     android:textColor="@color/primary_text" 
     android:textSize="16sp" 
     android:text="New Person" /> 
</LinearLayout> 

CardDialogView.cs

public class CardDialogView : MvxDialogFragment<CardDialogViewModel> 
{ 
    public override Dialog OnCreateDialog(Bundle savedState) 
    { 
     ...... 
     return dialog ; 
    } 
} 

enter image description here

Wenn ich auf dem Textview klicken, es öffnet die NewItemViewModel. So weit, so gut, aber CardDialogView (Dialog) erscheint immer noch, ich frage mich, wie man Dialog ablehnt.

CardDialogViewModel.cs

public class CardDialogViewModel : MvxViewModel 
{ 

    public ICommand NewCalculationCommand 
    { 
     get 
     { 
     return new MvxCommand(() => ShowViewModel<NewItemViewModel>(new { date = DateTime.Now })); 
     } 
    } 
} 

Antwort

3

Dies ist, was Sie tun können. In CardDialogViewModel fügen Sie eine neue Eigenschaft DialogVisible und es auf false gesetzt, wenn NewCalculationCommand ausgeführt wird:

public class CardDialogViewModel : MvxViewModel 
{ 

private bool dialogVisible; 

public bool DialogVisible 
{ 
    get {return dialogVisible;} 
    set {dialogVisible=value; RaisePropertyChanged(()=> DialogVisible));} 
} 

public ICommand NewCalculationCommand 
{ 
    get 
    { 
     return new MvxCommand(() => { 
      ShowViewModel<NewItemViewModel>(new { date = DateTime.Now }); 
      DialogVisible=false;); 
     } 
    } 
} 

Innen FundamentalView Uhr für DialogVisible Änderung und schließen Sie den Dialog:

CardDialogView dialog = null; 
CardDialogViewModel model = new CardDialogViewModel(); 
void OnCreate(Bundle bundle) 
{ 
    model.PropertyChanged += (sender, args) => 
    { 
     if(args.PropertyName == "DialogVisible" && !model.DialogVisible) 
     { 
      dialog.Dismiss(); 
     } 
    } 
} 

void OnAddButtonClick(object sender, System.EventArgs e) 
{ 
    dialog = new CardDialogView(); 
    dialog.ViewModel = model; 
    dialog.Show(((MainView)Activity).SupportFragmentManager, "CardDialogView");   
} 

Es kann einige Typen im Code sein aber ich denke, die allgemeine Idee ist leicht zu verstehen.

+0

@hotspring - Ich habe den Code in OnAddButtonClick korrigiert, um Felder aus FundamentalView zu verwenden – Giorgi

+0

Der zweite Teil Ihres Codes, der mit 'CardDialogView dialog = null;' beginnt, ist es in dem 'NewItemViewModel'? – hotspring

+0

@hotspring: Es ist in FundamentalView. Hier haben Sie OnAddButtonClick richtig? – Giorgi