ViewModel create in MVVM - my look at it - mvvm

When I want to implement ViewModel I should cut everything c# code from my Silverlight code behind(next delete .cs file and leave only .xaml file) and paste it in new class in new folder ViewModel ? It is good way of separate View from logic ?

When I want to implement ViewModel I should cut everything c# code from my Silverlight code behind
Not necessarily... MVVM isn't about "no code-behind". Sometimes it is useful or even necessary to use code-behind, if you're doing something that is specific to the view and is unrelated to the model/ViewModel.
and paste it in new class in new folder ViewModel
Definitely not ! If you do that, you're completely missing the point of MVVM. The ViewModel should have absolutely no knowledge of the View, so clearly you can't write the same kind of code in the ViewModel and in the code-behind...
I suggest you read some articles about MVVM, like this one by Josh Smith. The same Josh Smith also published a book about MVVM recently, which provides a good overview of MVVM

Related

MVVM, where to put/handle ui events such as 'onClick'

I know this is a vague question, but I was so fixated in MVP design, for years, I decided to keep myself up to date and decided to venture to MVVM architecture design, I've been reading alot of blogs, samples and stuffs about the Stream/Observer pattern that MVVM follows, but what is not clear with me(cant find very very simple code) how does MVVM handle ui-events? such as onclick? in MVP, the view and the presenter has a two-way contract to handle such thing
// called by view
presenter.onViewButtonClicked
void onViewButtonClicked() {
// do something here that business logic requires
view.doSomethingAfterPresenterReceivedClickEvent()
}
I know that View in MVVM subscribe's to ViewModel that when something happened to the data(Model) View will react to it
now, how can I tell ViewModel that I clicked something? ( PS : I know that I have to subscribe to ViewModel to listen to any changes when I clicked something ), I just need some guidelines and examples how can I tell VM that I did something intentionally.
I read Microsoft's MVVM documentation and it says something about iCommand, a contract-like something to tell MVVM a UI-event occurred.
would anyone enlighten me please... Thanks in advance
Edit: I forgot to mention, Im not using DataBinding, and due to fair pros and cons, I decided not to.
I am truly not an MVVM expert, but from my knowledge:
The main difference between MVP and MVVM is that in MVP, there is a two way connection between the view and the presenter, which means that the presenter knows about the view (interface) and the view knows about the presenter (interface).
In MVVM on the other hand, this connection is only one way, which means that the view knows about the viewmodel, but the viewmodel doesn't know about the view (since the connection from the viewmodel to the view is handled via observed data).
This allows the viewmodel to be independent of any view or view implementation and allows a single viewmodel to be used by multiple views.
So to (finally) answer your initial question: the way 'onClick' events are handled is basicly the same in MVP and MVVM .. the view notifies the presenter / viewmodel about the event and then:
the presenter sets up the data in the model and calls the according methodes on the view (in MVP)
the viewmodel sets up changed data which automatically triggers a reaction on the view since that data is observed (in MVVM)
Hope this helps :)

How to refactor viewmodels

Currently i am working on a project using prism where one of our modules has a viewmodel with 3000 lines of code!!!. really I would split this class to little pieces (some time is hard to read the code inside of this viewmodel)
Actually the code of the viewmodel has more less 30 properties related with commands and i think there it's good place to start.. (some idea how can i move these commands to other class?)
any ideas?
Thank you!
There are a few things you can do, firstly how much of the code should be in the view model, and how much of it should be in the model itself?
Secondly, can any of the code be part of services that the view model takes as a dependency?
Thirdly, you might want to consider a conventions based MVVM framework such as Caliburn.Micro which will allow you to use actions instead of WPF commanding, which means considerably less boilerplate code binding up verbs on your view model with events on your view, as well as other advantages.

How to switch view in MVVM using MEF

I have a singleton Model and ViewModel objects and would like to programmatically create and attach WPF views to them, one at a time. Views can be created dynamically, say by selecting a menu item (somewhere). Newly created view would dispose of any old view looking at a ViewModel. Then it would make itself a current view of that ViewModel, displaying it in some WPF window serving as a container for view UserControl. I am using MEF for IoC. It is important that Model and ViewModel objects are created only once. What would be the way to accomplish this using MEF?
You might have a look at the ViewModel and Writer sample applications of the WPF Application Framework (WAF). They show how to switch a view using MVVM and MEF.
I use viewmodel first approach in my testapps. so i instantiate viewmodel via mef and then wpf + datatemplates do the rest. all i have to do is binding my actual viewmodel to the contentcontrol.content.
you say that its important that ViewModel objects just created once. you achieve this with mef and creationPolicy.Shared or Lazy<> import. with this in mind i think ViewModel-First is the way you should go. its straightforward and you need no extra locator or wathever :)

Silverlight Navigation using Mvvm-light(oobe)+MEF?

What is the best approach for navigating between UserControls/Pages(out of browser experience)? I'm fairly new to Silverlight and even newer to the mvvm pattern.
How well does the Navigation Framework Integrate with the MVVM Light Toolkit? A snippet for general application flow control with the two would be great.
The plan was to use the Navigation Framework for general flow or using Jeremy Likeness's approach to region management(http://csharperimage.jeremylikness.com/search/label/regions) and swapping out regions as needed. I've seen a few places mention replacing the Visual Root, but that sounded like a hack to me.
Any advice, snippets, or a nudge in the general direction would be greatly appreciated.
Thank you.
You may find the following post useful:
SL4 Navigation Template with MVVM Light
I had the same problem and found this link (applies to Silverlight too):
Windows Phone 7 — Navigation between pages using MVVM Light Messaging
I'm struggling with the same thing, However, I am kinda steered away from the "NavigationService" because allegedly the journal keeps the object reference in memory or something I read somewhere.
I've been trying to utilize a frame in the main window, and using mvvm light to send navigation messages to the window to change the frame.
Edit: I figured it out.
In the main window (code behind, not viewmodel) I added a Messenger.Default.Register(Of NotificationMessage(Of NavMsg))...
Then in ANY view model I do a Messenger.Default.Send(Of Notificationmessage(Of NavMsg))...
And then I created a NavMsg class which accepts a string uri or an object to navigate to.
Here's some T4 code templates to generate you the code for your view/viewmodel to work with mvvmlight and it demonstrated the closing window message above. http://dl.activeaspsoftware.net/dl.aspx?f=MvvmLightCodeTemplates.zip

Opening an about box using MVVM pattern

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?
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.
Take a look at my Modal Dialogs solution for Silverlight 4:
Modal dialogs with MVVM and Silverlight 4
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.
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.
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