The Challenge of Embodying Spirit

As I sit here at Starbucks, headphones in, sipping my dark roast….I am having a hard time containing myself. I want to get up and dance to the music I am listening to. I don’t remember ever feeling…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Closing a window in MVVM pattern

But, imagine the following scenario: you have two buttons on your screen, one to save changes and close and another to cancel and close. But how do you close the window if your view model does not know what is the window and does not know how to handle UI logic?

To demonstrate it I will use MEF to keep a single instance of my EventAggregator across the system. For that I will annotate my class with the ExportAttribute (using System.ComponentModel.Composition;).

On my View I can get a instance of it. I will also implement IListen interface to be able to handle messages sent by others:

The CloseWindowMessage class:

I can then finally publish a message from my ViewModel notifying the view that it should close.

When the view receives the message it will call Handle which will close the window using the View’s logic for it. This way you have a view model decoupled from the view.

Add a comment

Related posts:

Greetings from New Jersey fellow Amerikans citizens

I wanted to take time out from my working, non-restful golf meetings and phone talkings to greet population. It is summer in New Jersey where I am which is part of Amerikan states. I know it is…

Senseless

Listen to the sound of silence It touches your ears gently Like a summer breeze. “Senseless” is published by Rodrigo Ferreira in Poets Unlimited.

Unimaginable

You say not possible. I say not yet. Learn to leave room for the unimaginable. The problem is never the problem. The problem is how you think about the problem. It takes a lot of energy to be…