MVVM in Windows Phone 7 - mvvm

Any good sample WP7 application using MVVM model in the optimal way?
I'm creating my first WP7 app, and I'm using MVVM as far as I can tell, but I'm not sure I'm doing it the right way. I have one view model per page, instead of one main view model that branches to each page, I'm not sure which is the correct way to do, so I'm hoping there's a sample app out there that I can check out.
Thanks!
Edit: I'm also having another problem on saving the ViewModels in the application state, because I think they have to be serializable (haven't worked much into this), the thing is that when I start a task and come back to the app, the latter has already been deactivated by calling the former, so I have to serialize its state when deactivated and [re]serialize it when [re]activated. This is how I save the state when deactivated:
object[] viewModels = new object[3];
viewModels[0] = App.ViewModelPage1;
viewModels[1] = App.ViewModelPage2;
viewModels[2] = App.ViewModePage3;
PhoneApplicationService.Current.State.Add("LastState", viewModels);
Again, this is probably not efficient way to do it, so I'm hoping I can see a sample app that handles this well too.
Thanks!

Have you looked at using the MVVM Light toolkit?
Serialization best practices will vary based on the volume of data in the model, the number of models being used and whether it's necessary to always load all of the models.

You might want to check out Caliburn Micro. It is used to implement a number of user experience patterns but it supports WP7 and has sample code.

Here's some MVVM samples and guidance you can check out.
C#er : IMage: Model-View-ViewModel (MVVM) Explained
.NET by Example: Using MVVM Light to drive a Windows Phone 7 / Silverlight 4 map viewer
The simplest way to do design-time ViewModels with MVVM and Blend.
Also an overview here of MVVM frameworks you may find worth a look.
JAPF » Blog Archive » Discover and compare existing MVVM frameworks
Light weight seems to be good and MVVM Light is popular. Laurent demos MVVM in the Mix 10 video EX14 if you want to check that out too.

This months MSDN magazine has an article on creating a WP7 Sudoko app using MVVM.
http://msdn.microsoft.com/en-us/magazine/gg490347.aspx
HTH

Here is my article which describes the approach to build WP7 applications using advantages of separation of concerns:
a framework for building of WP7 application

Related

What is the best design pattern between BLoC, MVVM, MVC for Flutter app?

I'm going to develop a management app for a company in Flutter and since I never worked with this framework i was looking for the best practices and desin patterns to use.
The app will be about employes management for a company. The main features will be:
The management of the employes profiles;
The possibility to upload documents directly in the app;
Create online quiz for the employes in trainership;
I'm struggling to choose the right design pattern. Usually for the frontend i use either the MVC or MVVM, but since this is my first Flutter app i made some researches and find out that one of the best pattern for Flutter is BLoC. I already tried to implement a simple app to try this pattern and i understood the way it works, but since I'm a noob in Flutter i was looking for some advices from someon who is more expert than me.
Thank you in advance for the help.
the best practices and design patterns to use it is TTD
it helps you to Keeping your code clean and tested
on the other hand, larger projects start falling apart when you mix the business logic everywhere. Even state management patterns like BLoC are not sufficient in themselves to allow for easily extendable codebase.
to get more information about TTD you can look to this tutorial in this link : https://resocoder.com/2019/08/27/flutter-tdd-clean-architecture-course-1-explanation-project-structure/

Why MvvmLight Over Mvvm in Xamarin.Forms?

While creating app in Xamarin Forms using Mvvm and MvvmLight, couldn't find significant reason of using MvvmLight. As all the features are already being provided by the xamarin forms/.net.
So if someone could point out the significance of MvvmLight (maybe with an example)?
This is probably just what people are used to using with WPF. Instead of re-learning the terminology of Xamarin.Forms Mvvm they stick with Mvvmlight.
There are probably other reasons. Mvvmlight focus solely on Mvvm where as Xamarin.Forms resources are more spread out so may not adapt to design pattern changes as quickly as MvvmLight.
If I were choosing an Mvvm library to use I would choose MvvmLight for the reasons stated above
I have been using MVVM light for some time, the one thing I love is that You can make UI changes to datatemplates during design time.
I am still new to Xamarin MVVM, but from what I can tell you have no design time preview in the shared project(Xamarin Forms) yet.
So MVVM light may not give you such an advantage.
Questions to ask yourself is why you would need an MVVM library in the first place.

Communicating between MEF objects

I'm building a workbench/IDE style application which uses MEF to locate and load tools into the workbench at start up.
I'm using MVVM Light to build the workbench and the separate tools. I've got a point now where I want tools to be able to communicate which each other and the workbench.
I don't mean direct communication, more like "Hey I have some data here if anyone's interested" and interested tools can pick up the data.
This looks much like C# events and I know I can export an interface from my tool(s) which defines the event(s) but I'm also wondering about using the MVVM Light messenger to provide the communication.
I've seen on here that people are using the EventAggregator from Prism to do the same thing and as the messenger from MVVM Light is roughly analogous I figured I could use it.
I'm after advice really, has anyone used the messenger for MEF - MEF object and would you advocate it over plain old C# events.
What you are looking at here is the mediator pattern. You could use plain old C# events to do the same thing... but do to it right you would most likely end up implementing your own kind of pub/sub pattern. And if you are going to do that, why re-invent the wheel?
I like using prism's EventAggregator because it is part of the Prism framework injected with MEF. I use a lot of other things from Prism, so it's nice to get this for 'free'. It also promotes loose coupling with your components, and like all of the good mediator implementations it is implemented with the weak event pattern which prevents the memory issues you would need to avoid yourself when destroying objects that have subscribed to an event.
For what it's worth, I used to use MVVM Light, but I found Prism to be a more mature framework, with better documentation, examples and a larger user base to learn from. That's really just my opinion- I know loads of people who love MVVM Light. Play with them both and pick the one that makes you happy :)

building an app to cater for WP7,Iphone & Android

I am about to start building an app that will be used across all platforms. I will using monotouch and monodriod so I can keep things in .net
I'm a little lazy so I want to be able to reuse as much code as possible.
Lets say I want to create an application that stores contact information. e.g. Name & Phone number
My application needs to be able to retrieve data from a web service and also store data locally.
The MVVM pattern looks like the way to go but im not sure my approach below is 100% correct
Is this correct?
A project that contains my models
A project that contains my views,local storage methods and also view models which I bind my views to. In this case there would be 3 different projects based on the 3 os's
A data access layer project that is used for binding to services and local data storage
Any suggestions would be great.
Thanks for your time
Not specifically answering your question, but here are some lazy pointers...
you can definitely reuse a lot of code across all 3 platforms (plus MonoWebOS?!)
reusing the code is pretty easy, but you'll need to maintain separate project files for every library on each platform (this can be a chore)
MVVM certainly works for WP7. It's not quite as well catered for in MonoTouch and MonoDroid
some of the main areas you'll need to code separately for each device are:
UI abstractions - each platform has their own idea of "tabs", "lists", "toasts", etc
network operations - the System.Net capabilities are slightly different on each
file IO
multitasking capabilities
device interaction (e.g. location, making calls etc)
interface abstraction and IoC (Ninject?) could help with all of these
The same unit tests should be able to run all 3 platforms?
Update - I can't believe I just stumbled across my own answer... :) In addition to this answer, you might want to look at MonoCross and MvvmCross - and no doubt plenty of other hybrid platforms on the way:
https://github.com/slodge/MvvmCross
http://monocross.net (MVC Rather then Mvvm)
Jonas Follesoe's cross platform development talk: Has to be the most comprehensive resource out there at the moment. He talks about how best to share code and resources, abstract out much of the UI and UX differences, shows viable reusable usage of MVVM across platforms and nice techniques for putting together an almost automated build. (yes, that includes a way for you to compile you monotouch stuff on Visual Studio)
Best of all he has a available source code for the finished product and for a number of the major component individually placed in its own workshop project and a 50 + page pdf detailing the steps to do so.FlightsNorway on github
IMO the only thing missing is how best to handle local data storage across all platforms. In which case I would direct you to Vici Cool Storage an ORM that can work with WP7, MonoTouch and (while not officially supported) MonoDroid.
*Disclaimer* The site documentation isn't the most updated but the source code is available. (Because documentation is Kriptonite to many a programmer)
I think the easiest way to write the code once and have it work on all three platforms will probably be a web-based application. Check out Untappd for example.
You can start by looking at Robert Kozak's MonoTouch MVVM framework. It's just a start though.
MonoTouch MVVM

How to: Multi screen/regions Silverlight application?

I have to create now a multi-screen Silverlight 4 RIA application with MVVM.
Each of these screens has to be devided in multiple regions (for example master-detail scenario whereas each of them is a different section and one has control on the other).
Can you give me some ideas what should be the right way to implement such an application?
Is Prism the right choice? I started reading the Prism manual and liked very much the idea of having regions and screens switched and controls in a very flexible manner, but, as said above, I find it too overkill to split it all over many assemblies.
If Prism IS the right choice, then I would appreciate any kind of guidance or reference to guidance on this particular scenario (multiple screens & regions and OTOH not getting my solution spotted with a gazillion projects.
I believe that PRISM is the correct choice for building an application with multiple regions and views. You could argue that that is almost the definition of a composite application.
But I would also remember that you don't have to use all the components PRISM has, you can pick and choose. I would recommend that you consider each aspect of PRISM and test/prototype to ensure you are happy with the facilties offered. In a large application I have built I use PRISM but after some prototype investigations I only used EventAggregator and the Modularity capabilities.
I chose not to use the region support as I found working with ItemControl and ContentControl components in Silverlight gave me the ability to inject views and partial views into my interface.
I found experience from ASP.NET MVC proved useful in considering how to coordinate/break up my UI into partial views.
Hope that helps. The Stocktrader application is a great example to learn from (included with the PRISM distributable).