instantiate/Call other Views(WPF forms) with their ViewModels using MVVM and Unity - mvvm

I'm using MVVM and Unity, I've understrood how to show the shell View (MainView with its MainViewModel) but I couldn't find the right way to instantiate other windows, for example : Details Button that opens a new form and show other details.
So, I'm looking for a common way how to instantiate/Call other Views(WPF windows) with their ViewModels using MVVM and Unity.

This answer may help with understanding how to link multiple views/viewModels together. I don't typically find myself needing to open additional windows just displaying different views in the current window.
Please let us know if you're looking specifically for an MVVM solution for opening new windows.

Take a look at this answer: Handling user interactions in MVVM. You can utilize an interaction service to instantiate new WPF windows will still remaining decoupled.
You can also provide indirect communication in WPF by leveraging the Mediator pattern to publish a message from a view model that causes a new view to be instantiated. This answer Simple Mediator implementation gives a quick overview.
I recommend you read over the User Interaction Patterns guidance, as it covers many of the scenarios you will face when using MVVM.

Related

How to implement multiple view for single presenter widget

I am using GWT-Platform and I read in PresenterWidget document that it is possible to implement multiple views with single PresenterWidget.
I am not getting idea how should I do that? Can anyone please provide insights how to so that?
Any working example will help better.
Thanks.
As mentioned in the comment, you can find a sample on how to achieve that, here
I believe their strategy is to group their presenters along with different GIN modules (i.e. Presenter X with view A1 go to GinModule1, Presenter X with view A2 go to GinModule2, etc).
Then they install one GIN module or the other, depending on some user agent parameter (in GWT you can do deferred binding via *.gwt.xml file).
This works well for them because they have this setup so they can have different views for mobile, tablet, desktop, etc, but the same presenters; therefore, the binding based on user-agent works well).
On another note, I think it is possible to bind through other mechanisms, but you need advanced GIN-fu skills here, and it's not really my area (but I'm fairly sure a colleague of mine mentioned this recently).

Caliburn + Xamarin navigation

Background:
We're using Caliburn.Micro 3.0.0-beta2 with a WPF + Xamarin iOS / Android project and we want to ensure the view models can be cross platform to reuse as much code as possible.
We've been looking at how to abstract our our navigation to be testable, cross platform and so callable in the view model rather than the views.
I was wondering if anyone had suggestions about how we should handle our app navigation. Our screens require us to inject some data such as database ID's into screens as we're navigating around. Currently we've done this with a view first approach by injecting the data into the view and having that pass it into the view model, but this doesn't feel ideal as it should really go into the view model as it's view logic (right?).
The view model is created for the view view using ViewModelLocator.LocateForView(this); and this then satisfies the rest of our dependencies using the SimpleContainer.
We Understand that navigation hasn't been implemented into 3.0.0 yet according to https://github.com/Caliburn-Micro/Caliburn.Micro/issues/142. We're really looking for a way we could do navigation that may be similar to the Caliburn.Micro solution implemented soon, with a hopeful view to contribute towards this if possible.
Questions:
How would we go about getting the data into the view model rather than the view to make it easier to test and more similar to how our WPF application will need to work?
Is this even sensible for a mobile app or should we be taking a view first approach? If so, what would be a testable approach for this?
Unless you have setup Caliburn.Micro for view-first approach (can be done there is a sample for this) the framework is almost completely viewmodel first in nature. I find it rather difficult to work in view first. Most of my "screens" are developed around the view model in question. With that in mind I also at times have multiple views for each viewmodel depending on the operation (add/ edit/ details/ list) for example.
1) DI, usually through repository or other context of that nature then using BindableColllection to hold and notify of changes to the view. Pretty much the same as WPF would work. Most recently my code has started to be cross platform (WP to WPF, now more Universal) to help reduce my headaches. Most of the patterns used are DI(SimpleContainer), Repository (EF 6x), Pub/Sub (IEventAggregator). The one exception is Repository but to an extent I still use it on WP but since I use Sqlite, up until recently EF was out of the question (EF 7 to the rescue)...
2) Do what is comfortable. If you are use to using Fakes then do it. It shouldn't matter as long as you have the correct results you are looking for in the end. Of course I am sure each test is going to be slightly tweaked for the platform they are tested on. Since each platform has its own nuances that you have to take into account.

MvvmCross: Application wide view model?

I am loving MvvmCross so far, but I am new to the MVVM technique. MVVM seems to center around the View and ViewModel and navigating between them. However, what about application-wide model items? Maybe my application has a mode that it can be in that affects all views and viewmodel behavior. This seems like an ApplicationModel or ApplicationViewModel. Or maybe just use the App class itself to store application wide stuff? What is the recommended practice for this concept? If using the App class itself is a good idea, I assume there is an easy way to get a hold of the reference to the App instance from anywhere? Haven't looked yet.
A ViewModel is a Model for a View - so that's where the current MvvmCross focus sits.
For this application wide behaviour, I think it's best to consider it one use case at a time.
The example you've provided is:
Maybe my application has a mode that it can be in that affects all views and viewmodel behavior.
There's not much detail here, but for this type of thing I might perhaps:
place this Mode inside a Singleton service
would use a messenger to send ModeChangedMessages when the Mode changed
would provide that service and the messenger to the relevant ViewModels using constructor injection
the ViewModels can then subscribe for ModeChangedMessage on the messenger
would perhaps use inheritance in my ViewModels to share code between them (ie they'd inherit from a BaseViewModel class)
There are of course other ways to do this, but that's one suggestion
If there's some other application wide use case you'd like to ask about, please ask another question - but please include more detail - eg perhaps provide some pseudo-code about what you want to share. I find real use cases easier to work out - abstract ideas are harder to talk about.
If it helps:
There's an introduction to services and constructor injection in N=2 on http://mvvmcross.wordpress.com
There's an introduction to the Messenger on N=9 in http://mvvmcross.wordpress.com

Understanding MVC pattern used in iOS apps

I have read Apple's MVC article and am confused about various things. Firstly Apple uses a combination of the View and the Controller in almost all its sample applications which is fine and I like it but they contradict themselves in this article because they said that View's should not rely on Controllers etc.
My main question is does anyone have a link to one of Apple's sample iOS projects which is a good example of the MVC pattern - with data retrieval etc because I don't fully understand the Model part of the pattern.
I don't understand the difference between a 'domain object' and a model object. For example if I wanted to retrieve a list of orders this would happen in a model class Orders. Would I then have another class Order which has properties such as OrderDate, OrderNumber etc or how would this work?
This sample code demonstrates a multi-stage approach to loading and displaying a UITableView. I think it's really interesting to dive in. It will show MVC in the works.
Here’s how the Model-View-Controller (also known as MVC) pattern maps to the main parts of your App:
Model → Data
View → User Interface
Controller → Core Logic
this explain fully with sample code
http://www.hollance.com/2011/04/making-your-classes-talk-to-each-other-part-1/
I believe that the following code will help you understand how to work with MVC in iOS application because its description says:
"MVCNetworking is a sample that shows how to create a network
application using the Model-View-Controller design pattern.
Specifically, it displays a photo gallery by getting the gallery's XML
description, thumbnails and photos from a web server, and uses Core
Data to cache this information locally."
http://developer.apple.com/library/ios/#samplecode/MVCNetworking/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010443
The model is the brain of the application. It does the
calculations and creates a virtual world for itself that can live
without the views and controllers. In other words, think of a model
as a virtual copy of your application, without a face!
A view is the window through which your users interact with your
application. It displays what’s inside the model most of the time,
but in addition to that, it accepts users’ interactions. Any
interaction between the user and your application is sent to a view,
which then can be captured by a view controller and sent to the
model.
Controllers in iOS programming usually refer to view controllers. Think of view controllers as a bridge between the model and your
views. They interpret what is happening on one side (what the user
does on the view side, or the information provided by the model) and
use that information to alter the other side as needed.
This is by far the best, yet simple explanation I've come across(taken from RayWenderlich)
"The idea behind MVC is that
- VIEWS should only care about how they are presented HOW THEY ARE PRESENTED,
- MODELS should only care about their DATA, - and CONTROLLERS should work to MARRY the two WITHOUT necessarily knowing too much about their internal structure."

Clean separation of UI with Caliburn MVVM

Looking into various MVVM frameworks for SL. In the Caliburn documentation I saw a code in a controller that calls MessageBox. Is this right or is this just for intro? Is there something like MessageBox service in Caliburn like in Chinch MVVM?
It's indeed introductive code, just to demonstrate that the controller method is actually executed. Yet, I agree on the issue you pointed out: the presence of raw UI code in the presenter could lead to an inappropriate mix of view concerns.
About the MessageBox service: in Caliburn v2 (trunk), ShellFramework module, there is a Question/Answer ViewModel abstracting the functionality of a MessageBox, with the advantage of letting you to design the UI for the dialog.
Also, it's very straightforward to roll your own IMessageBox abstraction and provide a basic implementation using the default WPF MessageBox.
Marco is correct. This is just to demonstrate that the action is called. I wouldn't recommend this in practice. I will try to make some changes to the samples or add some comments that make this clearer. Caliburn has services built-in for calling custom message boxes.