none
MVVM Strange Messenger Behaviour RRS feed

  • Question

  • I am trying to pass a User between two ViewModels. To do this I am using the MVVM light Messenger framework. Here is how I pass the 'User' when someone clicks a login button:

    #region Login
    private DelegateCommand _loginCommand;
    public ICommand LoginCommand
    {
        get
        {
            _loginCommand = new DelegateCommand(param => Login());
            return _loginCommand;
        }
    }
    
    private async void Login()
    {
        var waitWindow = new PleaseWaitWindow();
        waitWindow.Show();
    
        if (await LoginCheck())
        {
            // TODO: Navigate to the DashboardView
            Messenger.Default.Send(new ChangePage(typeof(DashboardViewModel)));
            Messenger.Default.Send(_chosenUser);
        }
    
        waitWindow.Close();
    }
    #endregion

    Here is how I subscribe to that message and use its contents:

    public DashboardViewModel()
    {
        Messenger.Default.Register<User>(this, OnUserReceived);
        Messenger.Default.Register<ChangePage>(this, ChangePage); // Register to Messenger to change Views hosted in ContentControl
    }
    
    private void OnUserReceived(User user)
    {
        FirstName = user.FirstName; // Breakpoint is hit here with correct value
        LastName = user.LastName;
    }

    And the two TextBlocks that are bound to the FirstName and LastName properties..:

    <StackPanel VerticalAlignment="Center" Margin="5">
        <TextBlock Text="{Binding FirstName}" Foreground="White" Margin="5" FontSize="18"/>
        <TextBlock Text="{Binding LastName}" Foreground="White" FontWeight="Bold" Margin="5" FontSize="20"/>
    </StackPanel>

    If I set a breakpoint where I action the Messenger then FirstName and LastName are set to the correct values. However, the TextBlocks are not updated and their text remains null.

    I have implemented a test button on the DashboardView that simply sets FirstName ="Test"; and this does update the TextBlock to say Text so I am confused as to why passing in a type of User and using its FirstName and LastName doesn't work.



    Sunday, July 16, 2017 4:57 PM

All replies