1
votes

I want to update ViewModel from view in mvvmcross, Can i update it ? I have tried messenger pluging but it didnt worked for me.

2
The messenger plugin is typically used for this. If you can explain what didn't work, or show some code, perhaps we can help you. - Trevor Balcom

2 Answers

5
votes

What you can do is you can use the View's ViewModel property and cast it to your type of ViewModel. After that you can access everything that you want to execute or change.

A very simple example using Droid and Core projects would be:

MainView

using Android.App;
using Android.OS;
using Cirrious.MvvmCross.Droid.Views;
using Core.ViewModels;

namespace Droid.Views
{
    [Activity]
    public class MainView : MvxActivity
    {
        protected MainViewModel MainViewModel 
        {
            get { return ViewModel as MainViewModel; } 
        }

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.main_view);

            MainViewModel.ChangePropertyToEmpty();
        }
    }
}

And then our ViewModel:

using Cirrious.MvvmCross.ViewModels;

namespace Core.ViewModels
{
    public class MainViewModel : MvxViewModel
    {
        #region TestProperty

        private string _testProperty;

        public string TestProperty
        { 
            get { return _testProperty; }
            set { SetProperty(ref _testProperty, value); }
        }

        #endregion

        public void ChangePropertyToEmpty()
        {
            TestProperty = string.Empty;
        }
    }
}
1
votes

Another way to do is just typecast it in the view class.

MainViewModel model= (MainViewModel)this.ViewModel;
model.ChangePropertyEmpty()