我已经创建了MVVMCROSS Tab链接中提到的TAB控件。
对于Ex:我有两个标签ViewModels '搜索和过滤' FilterViewModel:要在View中绑定的国家/地区和州的结果属性 SearchViewModel:使用FilterViewModel中的选定过滤器列出可用结果。
如何更新Searchviewmodel从Filterviewmodel属性更改的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。请建议/指导如何传递数据。
答案 0 :(得分:1)
您的要求我认为基本上您是如何在视图模型之间进行通信的。 有几种方法可以做到这一点:
1.您可以拥有自定义消息并从FilterViewModel发送它以通知SearchViewModel。 请看这里的例子:
Executing UI Code from ViewModel on MVVMCross
http://slodge.blogspot.co.uk/2013/05/n9-getting-message-n1-days-of-mvvmcross.html
在您的情况下,您将拥有类似
的UpdateSearchResultsMessagepublic 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,它同时搜索和过滤用户站点。
但也许你在两个视图模型中拥有的功能远远超过你所展示的功能,在这种情况下,它可能证明分离是合理的。