在我的iOS项目中,我有三个页面A、B、C
应用程序从A --> B --> C导航。
我是否可以在A上发布一个事件,该事件将在页面B和C上接收,如果这两个页面已经订阅了该事件,但尚未显示?
发布于 2017-01-27 00:39:41
如果您在A和B上,而C尚未显示,则它们不能对任何事件进行活动订阅。因此,它们将不会接收事件。
同样,如果你想让它在Android上工作,你也不能依赖这个模式。
相反,我会考虑使用服务,这是一个简单的可解析的单例,您可以在其中存储内容,并让ViewModels将该服务注入到ctor中。
如下所示:
public interface IMyService
{
string Data { get; set; }
}
public class MyService : IMyService
{
public string Data { get; set; }
}然后在视图A的ViewModel中:
public class AViewModel : MvxViewModel
{
public AViewModel(IMyService service)
{
GoToBCommand = new MvxCommand(() => {
// set data before navigating
service.Data = SomeData;
ShowViewModel<BViewModel>();
});
}
public ICommand GoToBCommand { get; }
}视图B的ViewModel:
public class BViewModel : MvxViewModel
{
private readonly IMyService _service;
public BViewModel(IMyService service)
{
_service = service;
}
public void Init()
{
// read data on navigation to B
var data = _service.Data;
}
}或者,如果您只传递较小的值,例如Id,则可以使用请求参数:
ShowViewModel<BViewModel>(new { id = SomeProperty });然后在您的虚拟机中:
public void Init(string id)
{
// do stuff with id
}https://stackoverflow.com/questions/41877455
复制相似问题