I want to update ViewModel from view in mvvmcross, Can i update it ? I have tried messenger pluging but it didnt worked for me.
1
votes
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;
}
}
}