在MVVMCross中不会调用ViewModel

时间:2016-04-28 20:35:37

标签: 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.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 ;
   }
}

以下相应的ViewModel没有被调用?我想知道我错过了什么?

CardDialogViewModel.cs

public class CardDialogViewModel : MvxViewModel
{

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

1 个答案:

答案 0 :(得分:2)

您在 CardDialogView.axml 布局文件xmlns:local="http://schemas.android.com/apk/res-auto"

中遗漏了这个内容

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上设置viewmodel,如下所示:

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