Click here to Skip to main content
Click here to Skip to main content

Closing Windows and Applications with WPF and MVVM

By , 10 Aug 2012
 

Introduction

This article presents an MVVM conform implmentation of an application that demonstrates how:

  • Application Startup/Shutdown and
  • Window Open/Close

can be implemented:

I have researched different solutions for closing windows in WPF for a while and I found a number of approaches (which was at first confusing). There are suggestions on the Internet [2] where an attached behaviour in the MainWindow of an application is used to close a window via an attached property [3] which is bound to a ViewModel. My sample application takes advantage of that solution and adds on to it. The sample implements:

  • Closing an Application's MainWindow (it makes no difference whether this is initiated by the MainWindow, the ViewModel, or the Windows Operating System - the App object is always in full control of the situation) 
  • A dialog specific viewmodel that can be used to drive a dialog and receive a DialogResult
    The dialog viewModel also sports a facitility for evaluating user input and producing corresponding messages (information, warning, error etc...) if some input is not of the expected quality.  

Using the code   

Application Startup and Shutdown 

The sequence for the application start-up is to:

  • Instantiate an object of the App class (which is generated by default in a WPF project) and use a Application_Startup method in that object to: 
    • Instantiate an AppViewModel (sometimes also referred to as workspace)
    • Instantiate an application MainWindow
    • Attach the ViewModel to the DataContext of the MainWindow
    • and Show the MainWindow
private void Application_Startup(object sender, StartupEventArgs e)
{
  AppViewModel tVM = new AppViewModel();  // Construct ViewModel and MainWindow
  this.win = new MainWindow();

  this.win.Closing += this.OnClosing;

  // When the ViewModel asks to be closed, it closes the window via attached behaviour.
  // We use this event to shut down the remaining parts of the application
  tVM.RequestClose += delegate
  {
    // Make sure close down event is processed only once
    if (this.mRequestClose == false)
    {
      this.mRequestClose = true;

      // Save session data and close application
      this.OnClosed(this.win.DataContext as ViewModel.AppViewModel, this.win);
    }
  };


  this.win.DataContext = tVM; // Attach ViewModel to DataContext of ViewModel
  this.InitMainWindowCommandBinding(this.win);  // Initialize RoutedCommand bindings
  this.win.Show(); // SHOW ME DA WINDOW!
}

This startup sequence has the advantage that View and ViewModel have no references of each other but can still work together perfectly. This is especially advantageous if you have to maintain more than one MainWinodw and you have to instantiate a different MainWindow based on the currently used configuration or a command line option. Command line parameters can also be evaluated in the Application_Startup method. The methode can then prepare the AppViewModel accordingly, and start-up the application in the expected manner (evaluating command line parameters is not implemented in the sample though).

The App class represents an outer shell that is wrapped around the instances of Views and ViewModels of an application. That outer shell should becomes active whenever the application starts up or shuts down.

The application shut down sequence is the inverse of the start-up sequence. That is, the MainWindow tells the AppViewModel that it has received a request for closing the application, the ViewModel evaluates this based on the current situation and either rejects it or confirms the request. 

Whether the shutdown sequence can take place or not is routed through the App class:

this.win.Closing += this.OnClosing;

private void OnClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
  e.Cancel = this.OnSessionEnding();
} 

private bool OnSessionEnding()
{
  ViewModel.AppViewModel tVM = this.MainWindow.DataContext as ViewModel.AppViewModel;
 
  if (tVM != null)
  {
    if (tVM.IsReadyToClose == false)
    {
      MessageBox.Show("Application is not ready to exit.\n" +
                      "Hint: Check the checkbox in the MainWindow before exiting the application.",
                      "Cannot exit application", MessageBoxButton.OK);
 
      return !tVM.IsReadyToClose; // Cancel close down request if ViewModel is not ready, yet
    }
 
    tVM.OnRequestClose(false);
 
    return !tVM.IsReadyToClose; // Cancel close down request if ViewModel is not ready, yet
  }
 
  return true;
}

This cascade of events takes place when the user clicks the MainWindow close button. It is slightly, different when the user selects the File>Exit menu option or presses the ALT+F4 Keys. In this case, the routed command binding in App.xaml.cs is executed:

win.CommandBindings.Add(new CommandBinding(AppCommand.Exit,
    (s, e) =>
  {
    e.Handled = true;

    ((AppViewModel)win.DataContext).ExitExecuted();
  }));

...which invokes the ExitExecuted() method in the AppViewModel class:

if (this.mShutDownInProgress == false)
{
  this.mShutDownInProgress = true;

  if (this.OnSessionEnding != null)
  {
    if (this.OnSessionEnding() == true)
    {
      this.mShutDownInProgress = false;
      return;
    }
  }

  this.WindowCloseResult = true;              // Close the MainWindow and tell outside world
                                             // that we are closed down.
  EventHandler handler = this.RequestClose;

  if (handler != null)
    handler(this, EventArgs.Empty);
}

...the above code calls the OnSessionEnding method in the App class via the delegate method property of the same name. This is done in such a convoluted way because I want to make sure that all shut down requests are handled by one method (App.OnSessionEnding()).

The shutdown progresses by setting the this.WindowCloseResult = true; which in turn invokes the attached DialogCloser property, which in turn will closes the MainWindow via the bound boolean property (see MainWindow.xaml). 

There is also a third path of execution towards the shutdown of the application, which is to shutdown the Windows Operating system. This event iterates through each application and asks them whether they can shutdown or not. We have set the SessionEnding="App_SessionEnding" property in the App.xaml code to invoke the OnSessionEnding method when Windows shuts down. This way, Windows will no longer shut down if the OnSessionEnding method signals that we still have things to save on disk (unless user forces Windows shut down of course).

To summarize this section. There are several execution paths for shutting down the main window, and through that, the application. All these paths are based on the same evaluation function (OnSessionEnding in App class) and end in one shutdown function OnClosed to make sure that session data can be saved before the application is down for good.

  • The main window can send the Closing event to check whether close down is OK via OnClosing method in the App class.
  • The main window can execute the AppCommand.Exit routed command (via File>Exit or Alt+F4) to shutdown the application through the ExitExecuted() method in the AppViewModel class.
  • The Windows operating system can invoke the App_SessionEnding method in the App class to gracefully shutdown our application

Dialog Open and Close

The previous section has described one case in which the attached property DialogCloser is used to close the MainWindow through setting the WindowCloseResult property to true. This attached behaviour (AB) is actually designed such that it can be used like a DialogResult. It is null by default and is set to true or false when the user closes the application via Cancel or OK.

I am using this behaviour to implement a viewmodel that drives a dialog (or any other view) in an MVVM conform way such that the dialog will not close unless the user has input data of the expected quality. The important theme here is that we have a viewmodel which drives the view through its complete life cycle without using any code behind. To do this, we have to construct a ViewModel and View object, attach the former to the DataContext of the later and subscribe to the Closing event of the view. Further more, View and ViewModel should implement an OK and Cancel Command to do the corresponding processing when the user clicks either button.

That seems to be a lot of work - so we have to do this for every ViewModel that drives a dialog? Too painful I hear you saying. Too convoluted some already said [2] claiming that code behind is OK in some cases. Some of you have a point there but if the complexity could by abstracted away into a neat little class which then could be used in a viewmodel whenever a dialog (or any other view) is needed to process input based on a OK or Cancel?

The DialogViewModelBase class implements the above items (and a bit more) needed to drive a dialog through its life cycle. The class can be used to make user input validation a trivial task.

This dialog is shown through the ShowDialog routed command in the AppCommand class, which is bound to the ShowDialog method in the AppViewModel class. This method invokes the ShowDialog method in the Util class. Here, we instantiate the dialog, attach a copy of the UsernameViewModel object and initiate the OpenCloseView property.

The UsernameViewModel class instantiates the DialogViewModel class in its OpenCloseView property. The OpenCloseView property is bound in the UsernameDialog.xaml

So, when you click OK or Cancel (or use the Escape key) in the UsernameDialog then you are actually executing a corresponding command in the DialogViewModel class. Now, executing the OK command invokes the PerformInputDataEvaluation method, which in turn, invokes an external method (ValidateData) bound to the EvaluateInputData delegate property.

The signature of that method is to return true or false depending on whether there are problems in the user input or not. If there are problems, these can be detailled, classified, and returned with Msg objects in the out List<msg> parameter. This list of messages is then fed into the ListMessages property of the ObservableCollection<msg> type. These messages are then visible with icons because the XAML of the dialog makes use of the CountToVisibilityHiddenConverter to hide the messages StackPanel when there are no messages. And it also uses the MsgTypeToResourceConverter to convert the category of a message into an image resource that can be displayed in the list of messages [4].

This input evaluation and message listing can be disabled by simple setting the EvaluateInputData delegate property to null. The dialog will then close via ViewModel when the IsReadyToClose is true.

Part 2 Using a DialogService

I received controversial feedback when I published the first version of this article. Some people were missing a dialog service implementation (I get to that in this section) and others were saying this approach of implementing dialogs is an over-design. Looking back on it, and having seen some other articles on similar topics [5], - I have realized that I stumbled into a "CodeProject trap" that snaps shut because some topics, such as, MVVM, are not so well defined and some patterns are quit different when being applied to solve different requirements.

My initial idea was to simply publish a small code example that could be used to implement simple WPF applications. Although, the requirements:

  • unit test
  • code re-use, and
  • seperation of concerns

[6] are more than useful problems to solve, they were not part of my initial concerns. I have reviewed a lot of articles on MSDN, CodeProject, and elsewhere since then and have come to the conclusion that a dialog service is a more than a useful exercise.

The key-terms to know are "Inversion of Control" (IOC) and Dependency Injection (DI). Just enter "IOC", "Dependency Injection", or "DialogService" into the search engine here at CodeProject or elsewhere and you will find numerous explanations on that topic.

I followed the IOC track and found there are lots of frameworks on that topic: Unity, MEF, StructureMap, Castle.Windsor, AutoFac, Chinch, LinFu, Hiro, and Ninject, which explains why people are so tired of being shown yet another framework Smile | <img src= " />  

I am not going to implement and document yet another framework in this article. Instead, we are going to look at a simplified implementation of a DialogService to explain and document the IOC service pattern with a sample implementation. I found such a sample implementation here at codeproject [5], simplified it a little bit more, and applied it to my sample code as described next.

Disore's original implementation [5] implements a few services to define interfaces for:

  • Reading Person information from XML,
  • Displaying MessageBox contents,
  • Using common dialogs, such as, Open File or Folder Browsing, and
  • Using ViewModels with a DialogService.

I was interested in the last point 'Using ViewModels with a "DialogService", only. So, I downloaded his source code and removed everything that was unnecessary for the DialogService implementation. This left me with the classes in the "Service" and "WindowViewModelMapping" folder:

We like simple things, right? It turns out that using these classes for the first time is simple (thanks to Disore's article and source code). But understanding their correct application in a non-intimidating way is all but simple. So, lets walk through the code to see what we can learn here.

The ServiceLocator class is static which means that it is initialized and loaded as soon as the .Net Framework loads the corresponding namespace. The dialog service starts its timely existence in the App.Application_Startup of the App class. The following lines:

// Configure service locator
ServiceLocator.RegisterSingleton<IDialogService, DialogService>();
ServiceLocator.RegisterSingleton<IWindowViewModelMappings, WindowViewModelMappings>(); 

initialize two services:  

  • A DialogService - which is used to create and show a View for a ViewModel, and
  • A WindowViewModelMappings service to associate (map) a ViewModel class with its corresponding View class.

We can register these services in any way we would like but we can (obviously) not instantiate a View for a ViewModel unless there is a map (association) for both classes. Disore [5] shows different ways for creating this map in his article. In our case, though, we use the below map:  

public WindowViewModelMappings()
{
  mappings = new Dictionary<type,>
  {
////{ typeof(AppViewModel), typeof(string) } ////,
{ typeof(UsernameViewModel), typeof(UsernameDialog)}
  };
} 

to associate the UsernameViewModel class with the UsernameDialog class at run-time. This comes to life in the below AppViewModel function:

public void ShowUserNameDialog()
{
  UsernameViewModel dlgVM = null;

  try
  {
    dlgVM = new UsernameViewModel(this.mTestDialogViewModel);

    // It is important to either:
    // 1> Use the InitDialogInputData methode here or
    // 2> Reset the WindowCloseResult=null property
    // because otherwise ShowDialog will not work twice
    // (Symptom: The dialog is closed immeditialy by the attached behaviour)
    dlgVM.InitDialogInputData();

    // Showing the dialog, alternative 1.
    // Showing a specified dialog. This doesn't require any form of mapping using 
    // IWindowViewModelMappings.
    dialogService.ShowDialog(this, dlgVM);

    // Copy input if user OK'ed it. This could also be done by a method, equality operator, or copy constructor
    if (dlgVM.OpenCloseView.WindowCloseResult == true)
    {
      Console.WriteLine("Dialog was OK'ed.");
      this.mTestDialogViewModel.FirstName = dlgVM.FirstName;
      this.mTestDialogViewModel.LastName = dlgVM.LastName;
    }
    else
      Console.WriteLine("Dialog was Cancel'ed.");
  }
  catch (Exception exc)
  {
    MessageBox.Show(exc.ToString());
  }
}

The call to InitDialogInputData() could be abstracted away into the interface of the dialog service but I am not going into that because, you feel comfortable with this concept, you should just use one of the 10 IOC containers listed above.

Summarizing the concept - there is one static class, the ServiceLocator [7] which is used to register the DialogService class and the WindowViewModelMappings class. The result of that registration is stored in a dictionary object in the ServiceLocator class:

private static Dictionary<Type, ServiceInfo> services = new Dictionary<Type, ServiceInfo>(); 

The call to the dialogService dialogService.ShowDialog(this, dlgVM); then pulls out the mapping service from the ServiceLocator dictionary which in turn pulls out the corresponding view from the WindowViewModelMappings dictionary:  

public bool? ShowDialog(object ownerViewModel, object viewModel)
{
  Type dialogType = windowViewModelMappings.GetWindowTypeFromViewModelType(viewModel.GetType());
  return ShowDialog(ownerViewModel, viewModel, dialogType);
}

Now we have an implementation that makes sure that Views and ViewModels are not aware of each other. Thats good for unit testing - which turns out to be simpler than you think. Just get, for example, Disore's source code, download and install the NUnit setup and use the Graphical GUI to get started with unit testing in 5 minutes.

Using an implementation like this uncovers other possebilities as well. Consider, for example, an application with an advanced and a basic view, -or an application in which you need a viewing mode or an editing mode. You could simply implement such run-time changes by mapping/re-mapping views at run-time.

Points of Interest

I have learned to use delegate methods to execute a flexible piece of code within a complex system, exposing thus, exactly what I need to expose while keeping the complexity of the overall system hidden. I also learned that common functions, such as, driving a dialog through its lifecycle can be encapsulated in a class, which can be instantiated when that dialog (or view) is being shown. Using a ServiceLocator can drive this development even further towards a professional implementation. I now have pattern that I can simply apply without having to care about Window Close, Closing, or any other events, because I could just implement:

  • the OpenCloseView property, the Input Evaluation method, and the ShowDialog method
  • plus the required XAML

and I am done with another dialog. Oh, and all that is done in an MVVM conform fashion. Now why would that not be a good approach?

References 

History   

  • 09. August 2012 Added DialogService implementation section and advanced code sample
  • 30 June 2012: Initial version
  • 3rd of July 2012 (fixed a bug in OnClosing method and moved util.ShowDialog out of ViewModel namespace

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Dirkster99
Germany Germany
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionDownload CodememberJose Betances3 Nov '12 - 12:09 
I am trying to download the code but I unable to. Can you make that possible.
 
thanks
AnswerRe: Download CodememberDirkster994 Nov '12 - 9:33 
I just tried to downoad the code and it works fine for me - I even compiled it to make sure it works. Not sure what the problem is but it might be something with your local compter/Internet connection - try using a different computer and check whether your problem persists.
 
Cheers Dirkster
GeneralGood article!memberVolynsky Alex9 Aug '12 - 21:33 
+5!
GeneralRe: Good article!memberDirkster9910 Aug '12 - 0:28 
Thanks
GeneralRe: Good article!memberVolynsky Alex10 Aug '12 - 0:30 
Smile | :) You are welcome
ale44ko

Questionwelcome to: http://www.netetrader.commemberkuif8sa9s9 Aug '12 - 14:54 
welcome to: http://www.netetrader.com
 
The website wholesale for many kinds of
fashion shoes, like the nike,jordan,prada,****, also including the jeans,shirts,bags,hat and the decorations. All the products are free shipping, and the the price is competitive, and also can accept the paypal payment.,after the payment, can ship within short time.
 

free shipping
 
competitive price
 
any size available
 
accept the paypal
 
http://www.mineokmalls.com
 
jordan shoes $32
 
nike shox $32
 
Christan Audigier bikini $23
 
Ed Hardy Bikini $23
 
Smful short_t-shirt_woman $15
 
ed hardy short_tank_woman $16
 
Sandal $32
 
christian louboutin $80
 
Sunglass $15
 
COACH_Necklace $27
 
handbag $33
 
AF tank woman $17
 
puma slipper woman $30
 
http://www.netetrader.com
General[OT]Where does your "Convert XAML Vector Graphic to PNG" article go?memberShuqian Ying16 Jul '12 - 17:34 
I was in the middle of reading it and it suddenly was gone?
Having way too many emails to deal with? Try our SQLized solution: Email Aggregation Manager[^] which gets your email sorted, found and organized beyond known precision.

GeneralRe: [OT]Where does your "Convert XAML Vector Graphic to PNG" article go?memberDirkster9917 Jul '12 - 3:41 
I am not sure what the problem is. I have looked at it:
 
http://www.codeproject.com/Articles/422523/Convert-XAML-Vector-Graphic-to-PNG
 
and it looks good to me(?)
GeneralRe: [OT]Where does your "Convert XAML Vector Graphic to PNG" article go?memberShuqian Ying17 Jul '12 - 9:33 
I can load your article now but the download link http://www.codeproject.com/KB/WPF/422523/OpenIconLibrary.zip[^] does not lead to download after clicking (in fact all three links behave the same on my side. After trying to download packages in other articles, they return error as well, it could be something wrong on the server side (12:30 PM Pacific Standard Time)...
Having way too many emails to deal with? Try our SQLized solution: Email Aggregation Manager[^] which gets your email sorted, found and organized beyond known precision.

GeneralRe: [OT]Where does your "Convert XAML Vector Graphic to PNG" article go?memberDirkster9917 Jul '12 - 12:09 
hmm, I tried it for the second time and it works great for me Confused | :confused:
 
The fact that other articles do not work makes me suspecious and I wonder whether your problem is not of a more general nature.
 
If you can, try using different browsers or different computers. Your problem could be anything ranging from a Virus scanner to Operating/Browser security setting. I can upload the zip files to a SkyDrive or something like that or Email it to you. But I would not be surprised if you had a problem downloading the files from there...
GeneralRe: [OT]Where does your "Convert XAML Vector Graphic to PNG" article go?memberShuqian Ying17 Jul '12 - 13:15 
Yes, something weired is happening to my network. It should has nothing to do with your article ...
Having way too many emails to deal with? Try our SQLized solution: Email Aggregation Manager[^] which gets your email sorted, found and organized beyond known precision.

QuestionI have done this quite a bit in the past and just use a simple servicemvpSacha Barber2 Jul '12 - 22:46 
Which I talk about in my Cinch MVVM articles, which have been around for a while now (2009 yikes that was a while ago now(.
 
Read here : WPF: If Carlsberg did MVVM Frameworks: Part 3 of n
Sacha Barber
  • Microsoft Visual C# MVP 2008-2012
  • Codeproject MVP 2008-2012
Open Source Projects
Cinch SL/WPF MVVM

Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue
 
My Blog : sachabarber.net

AnswerRe: I have done this quite a bit in the past and just use a simple servicememberDirkster993 Jul '12 - 6:40 
I included your article in the list of references (should be published soon).
GeneralRe: I have done this quite a bit in the past and just use a simple servicememberKevin Marois3 Jul '12 - 9:09 
I'm completely at a loss as to why anyone would use a service for closing windows. That is WAY WAY WAY over engineered and adds a level of complexity that just isn't needed.
 
it provides a service called Cinch.IUIVisualizerService which is a fairly complex beast
 
Why?? It's as simple as
 
1) Get a reference to the window
2) Window.Close()
 
See my solution below.
 
This article is about closing windows, so unless I completely missed something here, there is no reason to use anything other than Widnow.Close.
If it's not broken, fix it until it is

SuggestionOverdesignmemberabdurahman ibn hattab2 Jul '12 - 21:20 
Nearly all MVVM frameworks and their buzzers are the classic examples of over-design ideology. Every single school boy wants to do its own piece of over-design.
 
- Didn't you do your own piece of over-design?
- No.
- You wasted your life, son.
 
Sigh | :sigh:
GeneralRe: OverdesignmemberDirkster993 Jul '12 - 5:56 
This article does not describe an MVVM framework but a technique - you have a problem with that (father)? ...and no I am not done over-designing because I simply wanted to find a workable solution to the problems discussed above.
 
If thats such a terrible thing then who are these people bookmarking the article? Are they not done reading about overdesign?
GeneralRe: Overdesignmemberabdurahman ibn hattab3 Jul '12 - 7:01 
Workable solution:
 
Window.Close()
 
Done. Solved. Hallelujah
QuestionHere's a REALLY Simple SolutionmemberKevin Marois2 Jul '12 - 8:12 
In your viewmodel, do this:
 
var window = Utilities.GetWindowRef("MyWindow");
window.Close();
 
and in the Utilities class:
 
public static Window GetWindowRef(string WindowName)
{
    Window retVal = null;
    foreach (Window window in Application.Current.Windows)
    {
        // The window's Name is set in XAML. See comment below
        if (window.Name.Trim().ToLower() == WindowName.Trim().ToLower()) 
        {
            retVal = window;
            break;
        }
    }
 
    return retVal;
}
 
You also need to make sure that in the XAML you specify the Name tag in the Window definition at the top.
 
<Window x:Class="MyApp.Views.MyView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window Closing Example" 
        Height="600" 
        Width="800"
        x:Name="MyWindow"
        WindowStyle="SingleBorderWindow"
        WindowStartupLocation="CenterScreen"
        Window.ResizeMode ="NoResize">
 
That's all there is to it.
If it's not broken, fix it until it is

AnswerRe: Here's a REALLY Simple SolutionmemberDirkster992 Jul '12 - 8:58 
hm, I have tried this really simple solution but there must be something missing here,because when I execute GetWindowRef from my ViewModel as you requested - then
there is only one Window (the MainWindow) in the collection:
 
Application.Current.Windows
 
So, what am I missing? Are you suggesting to instantiate every Window and pull it
out with the GetWindowRef function? How about implementing a service locator
(<a href="http://www.codeproject.com/Articles/70223/Using-a-Service-Locator-to-Work-with-MessageBoxes">Using a Service Locator to Work with MessageBoxes in an MVVM Application</a>)
 
but is that not a little bit over the top for such a small code sample?
GeneralRe: Here's a REALLY Simple SolutionmemberKevin Marois2 Jul '12 - 9:06 
In my MainWindowViewModel I open a Wizard window, like this:
 
var mainWindow = Utilities.GetWindowRef("MainWindow");
 
WizardView view = new WizardView();
view.Owner = mainWindow;
view.ShowDialog();
 
At this point there are 2 windows in the collection. Even if all that was open is the MainWindow, you should still be able to use this.
 
So, in your MainWindowViewModel, try this as a test...
 
//CTOR
public MainWindowViewModel()
{
    var mainWindow = Utilities.GetWindowRef("MainWindow");
    mainWindow.Close();
}
 
The main window should open, and the close. You won't see anything, but it'll work. Remember, you must set the Name property in the View's XAML.
If it's not broken, fix it until it is

GeneralRe: Here's a REALLY Simple SolutionmemberDirkster992 Jul '12 - 10:23 
Yes, sure, this works for the MainWindow as soon as I have constructed it in the App class. But I think thats fine since the App class is neither View nor ViewModel anyway.
 
I thought we were looking at the UsernameDialog.xaml which is in my project constructed from a ViewModel. That dialog cannot be created with your solution since it has not been constructed and I don't think that constructing all views in advance is such a bright solution Frown | :( br /> 
Its great to hear that it works for your wizard in your solution but please post a really simple solution that will work with the code that I have posted. Smile | :)
GeneralRe: Here's a REALLY Simple SolutionmemberKevin Marois2 Jul '12 - 10:45 
I don't understand what the problem is. Once a window is contructed, it's available in the Windows collection.
 
Here's[^] a small sample app I just did.
 
The MainWindowViewModel handles opening the ChildWindow. Both the MainWindow and the ChildWindow are closed using my technique.
If it's not broken, fix it until it is

GeneralRe: Here's a REALLY Simple SolutionmemberDirkster993 Jul '12 - 7:26 
Ah, I see now what you mean. You close the ChildWindow via the GetWindowRef function. Yes I couold do that but I think its better to go all the way and use a service that does both - the creation and the destruction (as Sacha has suggested in a different comment). I might be doing that in an advanced version later (once I understand how it works in Cinch).
GeneralRe: Here's a REALLY Simple SolutionmemberKevin Marois3 Jul '12 - 9:12 
See my response to Sacha
If it's not broken, fix it until it is

QuestionVote of 4memberGanesanSenthilvel2 Jul '12 - 7:29 
Vote of 4
AnswerRe: Vote of 4memberDirkster992 Jul '12 - 7:36 
Hi,
 
can you suggest improvements beyond the util class problem discussed in the other threads (maybe you've seen errors or false statements?). Please let me know what you think   Wink | ;)
QuestionView/ViewModel separationmemberbravo21 Jul '12 - 22:55 
Hi !!!
By MVVM pattern must be separation between view and viewmodel. Your Util class located in ViewModel folder. In your case Util is some type of controller that know how to instantiate View and ViewModel.
AnswerRe: View/ViewModel separationmemberDirkster992 Jul '12 - 7:34 
I moved the util class out of the ViewModel namespace - this should be visible soon as changes are usually published within 24h - do you have an suggestion to implement this part differently?
GeneralMy vote of 3memberbravo21 Jul '12 - 22:50 
Nice solution, but your Util class located in the ViewModel folder. I think that must be separation ViewModel and View, because your ViewModel know about View. Maybe in the Presentation level you can add some type of Controller to manage instance View/ViewModel. Thx.
GeneralRe: My vote of 3memberDirkster992 Jul '12 - 7:31 
I moved the util class out of the ViewModel namespace I repaired the links so they should be OK when the change is published for everyone (takes usually no longer than 24h)
 
Could you be more specific on the 'some type of controller part' in the presentation? I am completely lost on that reference. Can you refer to a different sample project/article that shows the priciple that you would have expected?
GeneralRe: My vote of 3memberDirkster9910 Aug '12 - 0:27 
I have added a second version with a DialogService implementation - thanks for your comment Smile | :)
GeneralRe: My vote of 3memberbravo210 Aug '12 - 2:19 
Very Nice solution.
I implemented ServiceDialog by using PRISM (Event Aggregator) from the ViewModel side and some singelton class from UI side, that listening (subscribed) to common event.
QuestionBroken linksmemberKlaus Luedenscheidt1 Jul '12 - 19:04 
you should check the links you placed in the article. [1] and [2] are broken.
AnswerRe: Broken linksmemberDirkster992 Jul '12 - 7:26 
Thanks for the hint: I repaired the links so they should be OK when the change is published for everyone (takes usually no longer than 24h)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 10 Aug 2012
Article Copyright 2012 by Dirkster99
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid