WindowsPhoneMvp Quickstart

alt WindowsPhoneMvp

WindowsPhoneMvp is an MVP implementation for Windows Phone, the MVP pattern works well on Windows Phone because of its testability and flexibility. Use WindowsPhoneMvp to take back control of development and focus on creating great apps.

Getting Started

Getting started on your first project is easy, you can download the project template from the Visual Studio Gallery which will add a project template and a Project Item Template.

alt New Project

This will then give you the project structure below. The base project contains a few examples of a few neat things to get you started, but you can simply remove those few lines of code.

alt Project Structure

By default WindowsPhoneMvp comes pre-wired with Autofac to resolve your Presenters, however, if you feel that using an IoC container is overkill you can easily switch it out by replacing these lines of code in the App.xaml.cs

var navigationHook = _container.Resolve<INavigationServiceContext>();
navigationHook.Setup();
PresenterBinder.Factory = new AutofacPresenterFactory(_container);

with

INavigationServiceContext navigation = new NavigationServiceContext(new NavigationParser(), RootFrame);
navigation.Setup();

Choosing a Presenter Binding

If we navigate now to the MainView.xaml.cs file you'll see that by default WindowsPhoneMvp uses attributes to determine how a Presenter should be discovered for the current View.

[PresenterBinding(typeof(MainPresenter))]
public partial class MainView : MvpPhoneApplicationPage, IMainView
{
}

If Attributes are not your thing then you can use the Alternative discovery strategy which can find presenters via an attached Property of the Xaml itself. To do this we simply add one line to the "InitializePhoneApplication()" method of the App.xaml.cs file like below:

PresenterBinder.DiscoveryStrategy = new AttachedPropertyPresenterDiscoveryStrategy();

Once this property has been set the Presenter Binding will now look to the Attached Property when it needs to bind a Presenter.

<Phone:MvpPhoneApplicationPage 
binder:Presenter.Type="DemoWindowsPhoneMvp.Logic.Views.Main.MainPresenter, DemoWindowsPhoneMvp.Logic">

At this time, you must one Binding strategy, they cannot be mixed.

Adding a view

One of the first things you may want to do once your new application has been setup, is start to add some views. If you have the template installed, then the process is pretty simple. First create a new folder for your view.

alt New View Folder

Then right-click and add a new Project Item for a Windows Phone MVP Page

alt New Windows Phone MVP Page

This sets the minimum number of files you'll need for a new View. One thing to remember here though is that the template will create views with the Attribute Presenter Bindings, so if you've chosen to use Attached Properties you'll need to quickly change this yourself.

alt New View Files

Navigating

Once the view is setup, its time to setup some navigation to get there. WindowsPhoneMvp does all it's navigation from the Presenter, the Navigation Service that contains these controls is done via the Navigation property.

alt Navigation Service

The two main navigation strategies that you can take advantage of is navigating immediately, and navigating by using Commands. To invoke navigation immediately to a view, the following snippet can be used.

Navigate.ToView<ISecondView>();

The flexibility here is that we don't need to know the Xaml path to a view, we always navigate to either the View implementation or the Interface of the view (as done in the above example).

The following two code example will result in the same thing:

Navigate.ToView<SecondView>();

or even using the base interface, which means you don't really need specific interfaces for every view, until you actually need to use it:

Navigate.ToView<IView<SecondViewModel>>();

Similarly, you can use the Navigation Service to setup Commands that you can use for navigation. Because commands are not executed immediately they will be ready to use when they are invoked.

Model.NavigatingToViewTwo = Navigate.ByCommand<ISecondView>();

At times you will want to pass parameters to the new view. To do this you are able to attach these via the parameter builder when setting up the navigation.

Navigate.ToView<IGpsView>(x => x.With("Activity", Model.SelectedActivity.Name));

Conclusion

In this short quickstart, you should now have seen how you can get new Windows Phone Mvp projects up and running by creating a new project, choosing a presenter binding strategy, creating new views and setting up navigation.

This is really only scratching the surface, Windows Phone Mvp has now passed version v1.0 which means its time to dig in and have a go. Find out more by looking at the links below.

WP7 WindowsPhoneMvp
Posted by: Brendan Kowitz
Last revised: 21 Sep 2013 12:13PM

Comments

No comments yet. Be the first!

No new comments are allowed on this post.