简体   繁体   中英

mvvmcross iOS Messenger -

Sorry in advance because I'm a newbie to this. I'm trying to trigger a method with a string in another viewmodel , and I've read that using MvxMessenger is basically my best option to do this. What I don't understand because I can barely find any documentation/sample code to get me on my way is how to accomplish this

Whenever I click on a button in FilterViewModel , I also want it to trigger a method in the SearchHistoryViewModel with a string from the FilterViewModel . Basically, if the SearchHistoryViewModel code is even correct, how do I send/Publish this message correctly?

FilterViewModel

public class SearchHistoryFilterViewModel : MvxViewModel
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value;
            RaisePropertyChanged(() => Name);
        }
    }

    public SearchHistoryFilterViewModel(IMvxMessenger messenger)
    {
        //_token = messenger.Subscribe
        //_messenger = messenger;
    }

    public IMvxCommand FilterCommand
    {
        get
        {
            return new MvxCommand(FilterByName);
        }
    }

    public void FilterByName()
    {
        //Whenever this method is triggered, send a message with the Name in it
        SearchFilterMessage message = new SearchFilterMessage(this, Name);
        //Send message
          ... ? /////////////////////
    }`

SearchHistoryViewModel

public class SearchHistoryViewModel : MvxViewModel
{

  //properties
  ...
  ...
    private readonly MvxSubscriptionToken _token; 


    //ctor
    public SearchHistoryViewModel(ISearchHistoryService searchHistoryService, IMvxNavigationService navigationService, IMvxMessenger messenger)
    {
        _searchHistoryService = searchHistoryService;
        _navigationService = navigationService;

  /*Subscribe - Whenever a SearchFilterMessage is received, trigger the 
  OnFilterMessage method */
        _token = messenger.Subscribe<SearchFilterMessage>((message => { 
  OnFilterMessage(message.FilterName); })
            );
    }

    //methods
  ....
  ....
  ....
    /* Do this Whenever the SearchFilterMessage is received*/
   public async void OnFilterMessage(string name)
    {
        HistoryItems = await _searchHistoryService.GetHistoryByName(name);
    }

Alright, so I was confused on how to declare the messenger itself, and use it without the tokens, not in the constructor. Solution was predictably easy :( Basically declare another ImvxMessenger and via injection set it, then call on that one to Publish it in another method

    private IMvxMessenger _messenger;
    public SearchHistoryFilterViewModel(IMvxMessenger messenger)
    {
        //_token = messenger.Subscribe...
        //messenger.Publish<SearchFilterMessage>(FilterByName());
        _messenger = messenger;
    }

    public void FilterByName()
    {
        Debug.WriteLine(Name);
        SearchFilterMessage message = new SearchFilterMessage(this, Name);
        //Send message
        _messenger.Publish<SearchFilterMessage>(message);

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM