Skip to content Skip to sidebar Skip to footer

Passing On Variables From Viewmodel To Another View (mvvmcross)

For the past couple of weeks I've been working on developing a cross platform app (IOS/Android/WP7) using the MVVMCross framework. Today I ran into a problem I don't really know ho

Solution 1:

MVVMCross is very convention based - and it works on the idea of passing messages between ViewModels wherever possible.

If you navigate to a ViewModel using:

KeyValuePair<string,string> kvpAct1 = newKeyValuePair<string, string>("short", ".countertest5");

public IMvxCommand BeckhoffActuator1
{
    get
    {           
        returnnewMvxRelayCommand<Type>((type) =>this.RequestNavigate<Beckhoff.BeckhoffActuatorViewModel>(kvpAct1));
    }
}

then you should be able to pick that up in the BeckhoffActuatorViewModel using the constructor:

publicclassBeckhoffActuatorViewModel : MvxViewModel
{
    publicBeckhoffActuatorViewModel(stringshort)
    {
        ShortValue = short;
    }

    privatestring _shortValue;
    publicstring ShortValue
    {
        get
        {
            return _shortValue;
        }
        set
        {
            _shortValue = value;
            FirePropertyChanged("ShortValue");
        }
    }
}

And your views can then access ViewModel.ShortValue (for iOS this can be done after base.ViewDidLoad(), for Android after OnCreate() and for WP7 after OnNavigatedTo)

For an example of this, take a look at the TwitterSearch example:

This has a HomeViewModel which calls navigate using:

privatevoidDoSearch()
    {
        RequestNavigate<TwitterViewModel>(new { searchTerm = SearchText });
    }

and a TwitterViewModel which receives the searchTerm using the constructor:

    public TwitterViewModel(string searchTerm)
    {
        StartSearch(searchTerm);
    }

Please note that only strings are allowed in this message passing at present - but you can always serialise your own objects using JSON.Net - or you can extend the framework - it's open source.

Please note that only strings, ints, doubles and bools are allowed in this constructor parameter passing at present - this is due to serialisation requirements for Xaml Urls and for Android Intents. If you want to experiment with navigation using your own custom serialised objects, then please see http://slodge.blogspot.co.uk/2013/01/navigating-between-viewmodels-by-more.html.

Also, note that if you want to use the anonymous object navigation (RequestNavigate<TwitterViewModel>(new { searchTerm = SearchText });) then you will need to make sure that an InternalsVisibleTo attribute is set - see https://github.com/slodge/MvvmCrossTwitterSearch/blob/master/TwitterSearch.Core/Properties/AssemblyInfo.cs:

[assembly: InternalsVisibleTo("Cirrious.MvvmCross")]

Further... not for the faint-hearted... and this isn't "good mvvm code"... but if you really want/need to access the MvxShowViewModelRequest data inside an Android activity, then you can extract it from the incoming Intent - there's an Extras string containing the request (see the deserialisation in CreateViewModelFromIntent in https://github.com/slodge/MvvmCross/blob/master/Cirrious/Cirrious.MvvmCross/Android/Views/MvxAndroidViewsContainer.cs)

Post a Comment for "Passing On Variables From Viewmodel To Another View (mvvmcross)"