1

i have multiple module(every module has his own project) and i try to share a object between each module.

The object is on the UserViewModel Module initialized but not on the other modules (Only after the property changed trigger). I tried it with the EventAggregator but it's not quit right. I miss something realy important here.

public UserClass User
{
    get { return _user; }
    set { SetProperty(ref _user, value);}
}

    First Module

 public UserViewModel(IEventAggregator userEventAggregator, 
  UnityContainer container)
{
   _userEventAggregator= userEventAggregator;
   _userEventAggregator.GetEvent<UserSentEvent>().Publish(User);
   User.PropertyChanged += UserOnPropertyChanged;
}

 private void UserOnPropertyChanged(object sender, 
  PropertyChangedEventArgs e)
  {
     _userEventAggregator.GetEvent<UserSentEvent>().Publish(User);
  }

Second Module
public UserDetailsViewModel(IEventAggregator userEventAggregator, 
 IUnityContainer container)
  {
    _userEventAggregator= userEventAggregator;   
    _userEventAggregator.GetEvent<UserSentEvent>).Subscribe(UserUpdate);
  }
  • What's the exception you're facing? What exactly do you want to achieve? – Haukinger May 20 '19 at 21:32
  • @Haukinger There is no exception. The User Object within the UserViewModel initialized but not in the UserDetailsViewModel (it creates its own object). Only if the UserOnPropertyChanged triggered the User object in UserDetailsViewModel is the same as the UserViewModel User Object. My goal is to have the same object in every module to work with it. – FootstepsOfTheWitch May 21 '19 at 05:40

1 Answers1

0

The User Object within the UserViewModel initialized but not in the UserDetailsViewModel (it creates its own object)

That's the wrong way of doing things, not the view model creates the model, but the view model is created from the model. You need a shared service accessed from both of your view models, and that service manages the model.

Example:

internal class SimpleInMemoryUserRepository : IUserRepository
{
    public User GetTheUser()
    {
        return _user;
    }

    public void DeleteTheUser()
    {
        _user = null;
    }

    public void CreateNewUser()
    {
        _user = new User();
    }

    private User _user;
}

And in your initialization code:

container.RegisterSingleton<IUserRepository, SimpleInMemoryUserRepository>();
Haukinger
  • 10,420
  • 2
  • 15
  • 28