locked
Messenger is fired twice MVVM RRS feed

  • Question

  • Hi All,

    I have a ViewModelA from where I'm sending message to ViewModelB to execute method with linq (placed in ViewModelB).

    I'm using here MVVM Light Messenger, but I have implement wrapper:

    Public NotInheritable Class GetIDMain
        Public Shared Sub Send(getId As String)
            Messenger.[Default].Send(getId, SendToExecute.GetIDMain)
        End Sub
        Public Shared Sub Register(recipient As Object, action As Action(Of String))
            Messenger.[Default].Register(recipient, SendToExecute.GetIDMain, action)
        End Sub
    End Class

    GetIDMain.Send is called from ViewModelA.

    In ViewModelB I'm registering this as: 

    GetIDMain.Register(Me, AddressOf getAi)

    My method:

    Public Sub getAi()
        Using entityconn As New TimerConn()
            Dim result As New tbl_Main_Timer() With {
                .FirstActivity = "Test1",
                .Enumber = "21385650",
                .Manager = "JD VC"}
            entityconn.tbl_Main_Timer.Add(result)
            entityconn.SaveChanges()
        End Using
    End Sub

    I've noticed that in the table I have duplicated records (different ID (withPK)). So, I think that this method, or entire message is executed twice.  I was trying to implement Cleanup() or Unregister, but still - I'm getting duplicates here.

    What is causing this? Am I missing something? Message is registered on ViewModelB under the constructor...I think that the problem. But I would like to ask you for any comment on that.

    Thank you in advance.



    Tuesday, June 16, 2015 6:20 PM

Answers

  • >>What is causing this? Am I missing something?

    Hard to say without having seen all of your code. Are you sure that the method gets executed twice? Did you put a breakpoint in there to find out? Make sure that you create only a single instance of your view model class and that you are only sending the message once.

    Please upload a reproducable sample of your issue for further help.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question.

    • Marked as answer by VoVoTT Thursday, June 18, 2015 10:54 AM
    Tuesday, June 16, 2015 6:25 PM
  • It's being constructed twice then.


    Hope that helps.

    Technet articles: WPF: Change Tracking; All my Technet Articles

    • Proposed as answer by noorbakhsh Wednesday, June 17, 2015 4:58 PM
    • Marked as answer by VoVoTT Thursday, June 18, 2015 5:17 AM
    Wednesday, June 17, 2015 12:57 PM

All replies

  • >>What is causing this? Am I missing something?

    Hard to say without having seen all of your code. Are you sure that the method gets executed twice? Did you put a breakpoint in there to find out? Make sure that you create only a single instance of your view model class and that you are only sending the message once.

    Please upload a reproducable sample of your issue for further help.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question.

    • Marked as answer by VoVoTT Thursday, June 18, 2015 10:54 AM
    Tuesday, June 16, 2015 6:25 PM
  • That's a very strange way to use messenger.

    I would move the register

    Messenger.[Default].Register(recipient, SendToExecute.GetIDMain, action)
    

    Put that directly in the constructor of ViewModelB.

    You presumably only have one instance of ViewModelB at a time?

    Double check you've only got that one register anywhere in the code.

    See what happens when you move it.

    As you do that you may well spot something.

    Put a debug.writeline in GetAI so you can see whether it gets called twice without using breakpoints to interfere with processing.

    If you still get two entries the same then look at where it's sent from.


    Hope that helps.

    Technet articles: WPF: Change Tracking; All my Technet Articles

    Tuesday, June 16, 2015 7:03 PM
  • ViewModelB is called twice...

    I need to check why on button click it is getting two instances of it.

    For view distribution I'm using ViewModeLocator.

    Thank you guys!

    Tuesday, June 16, 2015 7:59 PM
  • ViewModelLocator just supplies the viewmodel to a view.

    Is it two instances of ViewModelB, or are you ending up with two instances of GetIdMain registered for the message?


    Hope that helps.

    Technet articles: WPF: Change Tracking; All my Technet Articles

    Wednesday, June 17, 2015 7:55 AM
  • What I did here:

    Shared isRegistered As Boolean = False

    and in the constructor

    If Not isRegistered Then
       Messenger.[Default].Register(Of NotificationMessage)(Me, TimerViewModel.Token, AddressOf MessageType)
       isRegistered = True
    End If

    Now it is working as it should.

    Thank you.



    • Edited by VoVoTT Wednesday, June 17, 2015 12:48 PM code
    Wednesday, June 17, 2015 12:47 PM
  • It's being constructed twice then.


    Hope that helps.

    Technet articles: WPF: Change Tracking; All my Technet Articles

    • Proposed as answer by noorbakhsh Wednesday, June 17, 2015 4:58 PM
    • Marked as answer by VoVoTT Thursday, June 18, 2015 5:17 AM
    Wednesday, June 17, 2015 12:57 PM
  • Yes, for now I will use this solution, and once I will test everything I will try to solve this issue.

    Thank you very much for your support.

    Wednesday, June 17, 2015 1:03 PM