0

I have created a TAB control as mentioned in MVVMCROSS Tab link.

For Ex: I have two tab ViewModels 'Search and Filter'.
FilterViewModel: Country and State results properties to bind in View.
SearchViewModel: Listing the available results to View by selected filters from FilterViewModel.

How to update Searchviewmodel SearchListing function from Filterviewmodel property changed ?

Edit : My sample Core function with Messenger code. But missing some work around! Please suggest

public class SearchWOViewModel  : MvxViewModel
{
    private readonly ISearchService _serachwo;
    private readonly IMvxMessenger _messenger;

    public SearchWOViewModel(ISearchService search, IMvxMessenger Messanger)
    {
        _serachwo = search;           
        Search = new SearchViewModel(_serachwo);
        Filter = new FilterViewModel(_serachwo, Search, Messanger);
    }
    private FilterViewModel _Filter;
    public FilterViewModel Filter { get { return _Filter; } set { _Filter = value; RaisePropertyChanged(() => Filter); } }

    private SearchViewModel _Search;
    public SearchViewModel Search { get { return _Search; } set { _Search = value; RaisePropertyChanged(() => Search); } }

}

public class FilterViewModel : MvxViewModel
{
    private readonly ISearchService _filterwo;
    private readonly IMvxMessenger messenger;
    private readonly MvxSubscriptionToken _token;

    public FilterViewModel(ISearchService search, SearchViewModel searchViewModel,IMvxMessenger _messenger)
    {
        _filterwo = search; messenger = _messenger;
        searchViewModel = SearchViewModel;
        SiteDropDown();         
        EquipmentDropDown();
    }

    public void SiteDropDown() { String query = "UserSite"; _filterwo.UserSite(query, result => { SiteResult = result; messenger.Publish(new UpdateSearchResultsMessage(this)); searchViewModel.SearchListingWO(); }, error => { }); }
    private List<DropDownEquipment> _siteresult;
    public List<DropDownEquipment> SiteResult { get { return _siteresult; } set { _siteresult = value; RaisePropertyChanged(() => SiteResult); } }

    public void EquipmentDropDown() { String query = "FillEquipment"; _filterwo.FillEquipment(query, result => { EquipmentResult = result; }, error => { }); }
    private List<DropDownEquipment> _EquipmentResults;
    public List<DropDownEquipment> EquipmentResult { get { return _EquipmentResults; } set { _EquipmentResults = value; RaisePropertyChanged(() => EquipmentResult); } }

    private DropDownEquipment _SelectedSite;
    public DropDownEquipment SelectedSite { get { return _SelectedSite; } set { _SelectedSite = value; RaisePropertyChanged(() => SelectedSite); } }
    private DropDownEquipment _SelectedEquipment;
    public DropDownEquipment SelectedEquipment { get { return _SelectedEquipment; } set { _SelectedEquipment = value; RaisePropertyChanged(() => SelectedEquipment); } }
}

public class SearchViewModel : MvxViewModel
{
    private readonly ISearchService _WorkOrder;
    public SearchViewModel(ISearchService search)
    {
        _WorkOrder = search;            
        SearchListingWO();
    }

    public void SearchListingWO() { String query = "WORKORDER"; _WorkOrder.Listingwo(query, result => Results = result, error => { }); }
    private List<ListingWo> _results;
    public List<ListingWo> Results { get { return _results; } set { _results = value; RaisePropertyChanged(() => Results); } }

    private ListingWo _SelectedWO;
    public ListingWo SelectedWO { get { return _SelectedWO; } set { _SelectedWO = value; RaisePropertyChanged(() => SelectedWO); } }
}

public class UpdateSearchResultsMessage : MvxMessage
{
    public UpdateSearchResultsMessage(object sender) : base(sender) { }
}

Please refer the sample code in MySampleTabCode. Kindly suggest/guide how to pass data.

Community
  • 1
  • 1
Kathir
  • 137
  • 3
  • 19

1 Answers1

1

What you are asking I think is basically how do you communicate between view models. There are few ways to do that:

1.You can have a custom message and send it from FilterViewModel to notify SearchViewModel. Look here for examples:

Executing UI Code from ViewModel on MVVMCross

http://slodge.blogspot.co.uk/2013/05/n9-getting-message-n1-days-of-mvvmcross.html

In your case you would have an UpdateSearchResultsMessage like

public class UpdateSearchResultsMessage : MvxMessage
{
     public UpdateSearchResultsMessage(object sender) : base(sender) {}
}

You will send this message from the SiteDropDown() method:

public void SiteDropDown()
{
      var messenger = Mvx.Resolve<IMvxMessenger>();
      String query = "UserSite";
     _filterwo.FillUserSite(query, result => { SiteResult = result; messenger.Publish(new UpdateSearchResultsMessage(this)); }, error => { });
}

Follow the instructions from the two links on how to register the message and how the SearchViewModel handles the message.

2.Much easier: Pass the SearchViewModel reference to the FilterViewModel in constructor:

  public SearchWOViewModel()
    {
        Search = new SearchViewModel(_serachwo);
        Filter = new FilterViewModel(_serachwo, Search);
    }

public class FilterViewModel : MvxViewModel
{
   public FilterViewModel(ISearchWOService search, SearchViewModel searchViewModel)
   {
      _filterwo = search;
      searchViewModel = SearchViewModel;
      SiteDropDown();
   }

    public void SiteDropDown()
    {
       String query = "UserSite";
      _filterwo.FillUserSite(query, result => { SiteResult = result; searchViewModel.SearchListingWO(); }, error => { });
    }
}

A general observation: I don't know how your application works, but maybe consider instead of having two view models just one view model, SearchViewModel, which does both search and filtering the user site.

But maybe you have much more functionality in the two view models than you showed, and in this case it might justify separation.

Andark
  • 400
  • 4
  • 16
WriteEatSleepRepeat
  • 3,083
  • 3
  • 33
  • 56
  • How to use single view model in tab functionality for doing the above mentioned functionality. – Kathir Sep 25 '13 at 09:59
  • I already gave you two solutions. Use one of them first. Then you can think to refactor your code. – WriteEatSleepRepeat Sep 25 '13 at 10:04
  • For one ViewModel, you just need to move the code from the two view models to this view model. same code, same properties. – WriteEatSleepRepeat Sep 25 '13 at 10:07
  • Ok i will use the above mentioned methods and let you know. Thanks. – Kathir Sep 25 '13 at 10:19
  • I have worked with one view model which does both search and filter. My question: Android doesn't display the output for SearchView. How to set single viewmodel for multiple view? FYI:Kindly refer my sample code for Android Tab parent. – Kathir Sep 26 '13 at 08:50
  • Hi. Kindly suggest. Still facing the same issue. – Kathir Oct 18 '13 at 06:48
  • I don't know what's your problem exactly. – WriteEatSleepRepeat Oct 18 '13 at 08:55
  • 'Follow the instructions from the two links on how to register the message and the SearchViewModel handle the message' I am not able to find the exact instructions from those links to my case. How can i post the sample application for your reference? – Kathir Oct 18 '13 at 10:32