locked
Using PageFunction with MVVM RRS feed

  • Question

  • I'm trying to write a simple app which has a logon page done as a page function (returning the name of the person who logged on).  However the wiring of events and OnReturn seems to need to be in the code behind and this is making my View and ViewModel rather tightly coupled.  Is there a way of using MVVM with PageFunctions that doesn't involve so much coupling between View and ViewModel?

    I'm using Visual Studio 2010, VB.NET and the .NET Framework 4 Client Profile.

     

    Wednesday, June 9, 2010 9:46 AM

Answers

  • Since there does not seem to be a way to avoid having the PageFunction's Return event hander in the code-behind I have tried a different approach.  Instead of using PageFunction I use ordinary Pages and the Messenger class from Josh Smiths MVVM Foundation.  On the logon screen, when the 'Logon' button is pressed I send a 'UserLoggedOn' message along with the 'UserName'.  My main-menu screen is registered to receive the 'UserLoggedOn' event.  The ViewModels for both Logon and MainMenu are no longer coupled to their respective Views.

    I do however, have a third 'host' View (for hosting the logon and main-menu pages).  The ViewModel for this host View is responsible for determining initial and subsequent pages to display.  My current thinking on this is that View and ViewModel for the host are a bit of a misnomer and that perhaps Host and PageController would be more accurate.

     

    • Marked as answer by Linda Liu Friday, June 11, 2010 10:23 AM
    Wednesday, June 9, 2010 4:48 PM

All replies

  • You can bind your submit button on login page to a command which resides in viewmodel and you can call the page function from there and handle it on viewmodel, when the name of person comes there will be a property of your say "UserName" which you will set to person name coming from page function and you will bind your view's textbox (say for example) to UserName property. Thus when UserName will change it will display data on textbox.

    Note: You need to inherit your viewmodel with INotifyPropertyChanged and your property UserName will also raise its event.

    Wednesday, June 9, 2010 10:43 AM
  • You can bind your submit button on login page to a command which resides in viewmodel and you can call the page function from there and handle it on viewmodel, when the name of person comes there will be a property of your say "UserName" which you will set to person name coming from page function and you will bind your view's textbox (say for example) to UserName property. Thus when UserName will change it will display data on textbox.

    Note: You need to inherit your viewmodel with INotifyPropertyChanged and your property UserName will also raise its event.


    I already tried this, I get the following error: "The PageFunction's Return event handler is required to be an instance method on the parent page object."  This is even though I am creating the PageFunction in the ViewModel.

     

    Wednesday, June 9, 2010 12:04 PM
  • Can u just place pagefunctions prototype and the way you are calling.
    Wednesday, June 9, 2010 1:20 PM
  • Can u just place pagefunctions prototype and the way you are calling.

      Private Sub ExecuteLogon()
        Dim next_page As New LogonPage
        AddHandler next_page.Return, AddressOf LogonPage_Callback
        m_NS.Navigate(next_page)
      End Sub
    
      Private Sub LogonPage_Callback(ByVal sender As Object, ByVal e As ReturnEventArgs(Of String))
        MsgBox("Returned " + e.Result)
      End Sub
    If I have the equivalent code in the code-behind of the view it works, in the view model I get the "The PageFunction's Return event handler is required to be an instance method on the parent page object."  error.
    Wednesday, June 9, 2010 1:24 PM
  • Please move

    Dim next_page As New LogonPage
    AddHandler next_page.Return, AddressOf LogonPage_Callback

    Lines to class constructor and just call the Navigate event in ExecuteLogon() method.

     

    Wednesday, June 9, 2010 2:58 PM
  • Please move

    Dim next_page As New LogonPage
    AddHandler next_page.Return, AddressOf LogonPage_Callback

    Lines to class constructor and just call the Navigate event in ExecuteLogon() method.

     


    Exactly the same error: "The PageFunction's Return event handler is required to be an instance method on the parent page object."
    Wednesday, June 9, 2010 3:11 PM
  • Since there does not seem to be a way to avoid having the PageFunction's Return event hander in the code-behind I have tried a different approach.  Instead of using PageFunction I use ordinary Pages and the Messenger class from Josh Smiths MVVM Foundation.  On the logon screen, when the 'Logon' button is pressed I send a 'UserLoggedOn' message along with the 'UserName'.  My main-menu screen is registered to receive the 'UserLoggedOn' event.  The ViewModels for both Logon and MainMenu are no longer coupled to their respective Views.

    I do however, have a third 'host' View (for hosting the logon and main-menu pages).  The ViewModel for this host View is responsible for determining initial and subsequent pages to display.  My current thinking on this is that View and ViewModel for the host are a bit of a misnomer and that perhaps Host and PageController would be more accurate.

     

    • Marked as answer by Linda Liu Friday, June 11, 2010 10:23 AM
    Wednesday, June 9, 2010 4:48 PM