在MVVMCross中关闭DialogFragment

时间:2016-04-29 19:22:48

标签: android xamarin mvvmcross

FundamentalView.cs 中,我有一个click事件,它从视图底部触发一个片段,并带有选项(添加一个新人和新计算)。

 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

当我点击文本视图时,它会打开 NewItemViewModel 。到目前为止,这么好,但 CardDialogView (对话框)仍然出现,我想知道如何解除对话。

CardDialogViewModel.cs

public class CardDialogViewModel : MvxViewModel
{

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

1 个答案:

答案 0 :(得分:3)

这是你可以做的。在CardDialogViewModel中添加新属性DialogVisible并在执行NewCalculationCommand时将其设置为false:

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

FundamentalView内注意DialogVisible更改并关闭对话框:

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

代码中可能有一些类型,但我认为一般的想法很容易理解。