Opening an about box using MVVM pattern
Asked Answered
B

5

10

I'm working on a new WPF application and I'm trying to stay as close to the MVVM pattern as I can. My XAML files right now have no codebehinds, and all my interactivity is achieved using Josh Smith's RelayCommand class and commands in my ViewModel classes.

This worked great until I started working on the housekeeping tasks, such as an about box and a system preferences page. I want to have these as modal dialogs, but if I create a RelayCommand to open these pages, I'll be creating a dependency on the view within my view model.

This strikes me as against the grain of the MVVM pattern.

Is there an established method for creating new windows (modal and/or modeless) within the MVVM pattern without creating a dependency? It seems the only way I can keep the ViewModel clean is to create a Click event handler in the XAML codebehind file and create the new view within the old view.

Any recommendations?

Blinni answered 25/8, 2009 at 5:26 Comment(0)
S
7

One way to handle this is to implement a service that provides Views to ViewModels. Views register with the service and ViewModels can request dialogs from the service. This is an example of the Gang of Four mediator pattern.

Salicin answered 25/8, 2009 at 5:35 Comment(0)
U
1

Take a look at my Modal Dialogs solution for Silverlight 4:

Modal dialogs with MVVM and Silverlight 4

Ulda answered 21/1, 2010 at 21:32 Comment(0)
E
0

Please see my answer to this question about why the Window class itself is a ViewModel, so you can use it directly from your ViewModel without worries.

Elitism answered 25/8, 2009 at 12:5 Comment(0)
B
0

Laurent Bugnion has a weak-referenced mediator, in which he uses it to show dialog messages. Along with the message that is broadcast, a callback delegate is sent for the subscriber to execute. You could use the same concept to show an about dialog. Check out DialogMessage.cs from the source here.

Bocage answered 25/8, 2009 at 19:3 Comment(0)
L
0

We use Controller classes which are responsible for the UI Workflow. They create the modal windows and they mediate between various ViewModels.

How you can open a modal window with the View-Model-ViewModel (MVVM) pattern is shown in the ViewModel sample application here:

WPF Application Framework (WAF)

http://waf.codeplex.com

Lurlenelurline answered 29/8, 2009 at 10:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.