0

我创建了一个 TAB 控件,如MVVMCROSS Tab链接中所述。

例如:我有两个选项卡 ViewModels “搜索和过滤”。
FilterViewModel:要在 View 中绑定的 Country 和 State 结果属性。
SearchViewModel:通过从 FilterViewModel 中选择的过滤器列出可用的结果以查看。

如何更新从 Filterviewmodel 属性更改的 Searchviewmodel SearchListing 功能

编辑:我的带有 Messenger 代码的示例核心函数。但是缺少一些解决方法!请建议

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) { }
}

请参考 MySampleTabCode中的示例代码。请建议/指导如何传递数据。

4

1 回答 1

1

你问的我认为基本上是你如何在视图模型之间进行通信。有几种方法可以做到这一点:

1.您可以有一个自定义消息并从FilterViewModel发送它来通知SearchViewModel。在此处查看示例:

在 MVVMCross 上从 ViewModel 执行 UI 代码

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

在你的情况下,你会有一个 UpdateSearchResultsMessage 像

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

您将从 SiteDropDown() 方法发送此消息:

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

按照两个链接中关于如何注册消息以及 SearchViewModel 如何处理消息的说明进行操作。

2.更容易:将 SearchViewModel 引用传递给构造函数中的 FilterViewModel:

  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 => { });
    }
}

一般观察:我不知道您的应用程序是如何工作的,但可以考虑而不是只有一个视图模型 SearchViewModel,而不是两个视图模型,它同时搜索和过滤用户站点。

但也许您在两个视图模型中的功能比您展示的要多得多,在这种情况下,它可能证明分离是合理的。

于 2013-09-25T08:26:04.453 回答