casillas
casillas

Reputation: 16793

ViewModel does not get called in MVVMCross

In the FundamentalView.cs, I have click event which triggers a fragment from the bottom of the view with having options (adding a new person and new calculations).

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

The following corresponding ViewModel does not get called? I wonder what I am missing?

CardDialogViewModel.cs

public class CardDialogViewModel : MvxViewModel
{

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

Upvotes: 0

Views: 141

Answers (1)

Iain Smith
Iain Smith

Reputation: 9703

You are missing this from the CardDialogView.axml layout file xmlns:local="http://schemas.android.com/apk/res-auto"

CardDialogView.axml should be this:

<?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>

Think you need to set the viewmodel on the CardDialogView like so:

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

Upvotes: 1

Related Questions