Brade
Brade

Reputation: 415

Calling a function in a Window from a different Window's View Model

So, I currently have 2 windows (MainWindow, SubWindow) and a view model. The MainWindow is mostly just a taskbar icon using hardcodet.wpf.taskbarnotification.taskbaricon. Clicking on a button in the context menu of this taskbar icon opens up the SubWindow.

The SubWindow's DataContext is set to the view model. The SubWindow allows for the user to enter in a bunch of data and click an "Upload" button, which will send all of that data to a service I have written previously. This service will do a number of things with the data and return a single string. My goal currently is to take this returned string and use it in the MainWindow, however this is where my problem lies.

The call to the service is done in the ViewModel and I am unsure how to get the response back to the MainWindow. My idea initially was to just have references to each object in the others (ie. reference to MainWindow in SubWindow and SubWindow in ViewModel), however I would prefer to avoid doing this. I did also consider events however I have not been able to figure out how to get the events to work between the ViewModel and the MainWindow.

Any help/suggestions on how I should make the events or what else I should try to do this would be greatly appreciated. If any more information is required, let me know.

Upvotes: 1

Views: 191

Answers (2)

Ankur Tripathi
Ankur Tripathi

Reputation: 691

Use MVVM Light Messenger

> http://dotnetpattern.com/mvvm-light-messenger
public class ViewModelA : ViewModelBase
{
    public void SearchCommandMethod()
    {
        MessengerInstance.Send<NotificationMessage>(new NotificationMessage("notification message"));
    }
}

Jesse Liberty of Microsoft has a great concrete walk through on how to make use of the messaging within MVVM Light. The premise is to create a class which will act as your message type, subscribe, then publish.

public class GoToPageMessage
{
   public string PageName { get; set; }
}
This will essentially send the message based on the above type/class...

    private object GoToPage2()
    {
       var msg = new GoToPageMessage() { PageName = "Page2" };
       Messenger.Default.Send<GoToPageMessage>( msg );
       return null;
    }

Now you can register for the given message type, which is the same class defined above and provide the method which will get called when the message is received, in this instance ReceiveMessage.

    Messenger.Default.Register<GoToPageMessage>
    ( 
         this, 
         ( action ) => ReceiveMessage( action ) 
    );

    private object ReceiveMessage( GoToPageMessage action )
    {
       StringBuilder sb = new StringBuilder( "/Views/" );
       sb.Append( action.PageName );
       sb.Append( ".xaml" );
       NavigationService.Navigate( 
          new System.Uri( sb.ToString(), 
                System.UriKind.Relative ) );
       return null;
    }

Upvotes: 1

Dirk Reuschling
Dirk Reuschling

Reputation: 48

Have you tried passing a delegate (function callback) from the main window view model to the sub window? So the subwindow can call this method to pass back results to the main form.

Upvotes: 0

Related Questions