You can use MessageBus or EventAggregator to publish messages to subscribers using weak links. Take a look at my implementation (or the NuGet package ),
It can also march message processing for the user interface thread for you (if you need to update the user interface components) by applying HandleOnUIThreadAttribute to the Handle method.
Use in your case will look something like this:
// The message public class LoginModelChanged { public LoginModelChanged(LoginModel model) { Model = model; } public LoginModel Model { get; private set; } } // Service that publishes messages public class ModelProviderService { private IMessageBus _messageBus; private LoginModel _loginModel; public ModelProviderService(IMessageBus messageBus) { _messageBus = messageBus; } public LoginModel LoginModel { get { return _loginModel; } set { _loginModel = value; _messageBus.Publish(new LoginModelChanged(value)); } } } // Subscribing ViewModel public class SomeViewModel : IHandle<LoginModelChanged> { public SomeViewModel(IMessageBus messageBus) { messageBus.Subscribe(this); } public void Handle(LoginModelChanged message) { // Do something with message.Model } }
If you want to learn more about internal work and how to implement it; Check out the source code in the GitHub repository . Feel free to use the code as you wish :)
khellang
source share