View Model Page Navigation with MVVM Light

When you move from code behind to a View Model (as you should, if only to makeBlank freeway sign ready for your custom text testing easier) certain challenges arise, such as: how do I navigate to another page?

Fortunately, MVVM Light provides a Messaging bus, that makes it easy to send messages from a View Model to a page (or to another View Model).  This can be over done but when used judiciously it makes the very difficult into the very easy.

The following solution was improved enormously by Eric Grover, my favorite MVVM Light Guru after Laurent 🙂

Begin by creating a parent for ContentPage (I called mine BaseViewPage).  Have your content pages derive from the new page.  In the new page we’ll register to receive Navigation Messages from the Messenger Bus

 

public BaseViewPage( )
  {
 
      Messenger.Default.Register<INavigationMessage>(
          this,
          HandleNavigation );
 
  }

Notice we need an interface, INavigationMessage and a method, HandleNavigation.

INavigationManager looks like this:

 

public interface INavigationMessage
  {
      Type PageType { get;  }
  }

This allows us to navigate to any type of page.  Its implementation is:

public class NavigationMessage<T> : INavigationMessage where T : Page
{
    public Type PageType
    {
        get { return typeof(T); }
    }
 
}

This nicely forces the type to be a Page.

Here’s HandleNavigation, which also goes in the BaseViewPage:

 

public async void HandleNavigation( 
    INavigationMessage navigationMessage )
   {
 
       var target = Activator.CreateInstance(
               navigationMessage.PageType);
 
       await Navigation.PushAsync( (ContentPage) target );
   }

Nicely generic, which is just what we want.

Now we’re ready to use this.  Let’s say we’re in MyPageViewModel and we want to navigate to YourPage (xaml).  We write:

 

var message = new NavigationMessage<YourPage>();
Messenger.Default.Send( message );

Slick, eh?

Share
Posted in Xamarin | 1 Comment

Yet Another Podcast #154 – Dan Wahlin on Typescript and Angular 2

Dan Wahlin founded Wahlin Consulting which provides consulting and training IMG_0510services on Web technologies.

He has also published over 10 developer courses on Pluralsight.com and Udemy.com including the new Docker for Web Developers course. Dan is a Docker Captain, Google GDE, Microsoft MVP and Regional Director and speaks at conferences and user groups around the world.

Dan has written several books on Web technologies, hundreds of technical articles and blog posts and runs the Web Weekly newsletter – a great way to stay up on the latest technologies. Follow Dan on Twitter @DanWahlin.

 

 


Share
Posted in Agile, Essentials, TypeScript | Tagged | 2 Comments

TypeScript and Angular 2

AngularAfter attending ng-conf a couple weeks ago, and taking a terrific workshop with John Papa and Dan Wahlin, and spending time talking with them and with Ward Bell (who is responsible for the Angular 2 documentation), I’ve decided to go all-in with Angular 2 for my Web work.

Angular 2 is written in and all the samples and documentation are written in TypeScript.  Thus, TypeScript is my next language of choice.  This should be pretty straight forward, as the hard part is OOP, which we C# programmers are already comfortable with.

I’ll try to document my travels through these technologies as I go.  Truth is that I’m fully employed with Xamarin work, so this will come in fits and starts, but I’ll do what I can to keep up a cadence.

Looking forward to it.

Photos from ng-conf

Share
Posted in Angular, TypeScript | Leave a comment

Global Resources in Xamarin.Forms

First, when I checked Google, all I could find was articles saying this can’t be done.  Humbug.  Thanks to Roger Peters and jFarrell I got it done (well, they did and then I did, but now you will).

Here are the steps (and they are pretty easy)

  • Create a normal XAML file named App.xaml.
  • This will automatically create a file named App.xaml.cs
  • Copy from App.cs to App.xaml.cs and delete App.cs
  • In App.xaml set the ContentPage to be an Application and create a resource dictionary
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="eSiteMoble.App">
 
 
  <Application.Resources>
    <ResourceDictionary>
  • Add the keyword Partial before class in App.xaml.cs
  • Add InitializeComponent to the constructor (this is critical)

Hey! Presto! your resources are available to any page…

<Label Text="Remember Me?"
             TextColor="{StaticResource TextForegroundColor}"

Easy as cake. Piece of Pie.

Share
Posted in Xamarin | Leave a comment

Reactive

Many web programmers, especially Angular programmers are turning their attention to RECoverReactive programming.  While I don’t know of any books on ReactiveJS (yet) I can shamelessly mention that I do have a book on Reactive Extensions and LINQ.

 

NB: This book is short (which I think is a good thing) and pricey, but will give you a solid introduction and more to both topics.

My co-author, Paul Betts, is not only a genius, but he is one of the foremost experts on Reactive.

Here is the TOC:

RETOC

Share
Posted in AngularJS, JavaScript, Linq, Programming, Reactive | Tagged | 2 Comments

Video of Evolve 16 Presentation on TDD

Pleased and proud to say that Xamarin has posted a video of my presentation on Test Driven Development on their site.   Evolve 16 was an incredible event, and it was tremendous fun to present there.

IMG_0371

Share
Posted in Xamarin | Leave a comment

Back to the future!

For about 15 years I specialized in Web programming of one sort or another (writing half a dozen books and, more recently, half a dozen Pluralsight courses) and for the past two years, I’ve added Xamarin.

These tEvolvewo interests came together this month.  I had the privilege of presenting at Evolve (the largest mobile conference) and then I had the pleasure of attending NG-Conf, the largest conference on Angular.

At Evolve there were a number of announcements about Xamarin, and I had a good time talking with friends (the hallway conversations are always the most valuable part of any conference)

The schedule and speakers were great, and the excitement and energy in the Keynotes was terrific.

The very next week, I attended NG-Conf.  I started out with a full-day workshop presented by two of my friends, John Papa and Dan Wahlin (see photo below).

NG Conf  is interesting because Wednesday and Friday are single-track, with each presentation lasting 20 minutes.  This causes (forces?) the presenters to get to the point and not to waste any time at all.  
IMG_0461

Angular 2 is a very exciting development in front-end Web technology.  It is quite different from, and in my opinion a significant improvement over Angular 1.

Stay tuned for more details on Angular 2 as it settles down and nears version 1.0.

 

 

 

Share
Posted in Angular, Essentials, Xamarin | Leave a comment

Yet Another Podcast #153 – James Montemagno: Xamarin

James Montemagno is a Xamarin Evangelist, technology geek, and lover of all things mobile.  This is a great wrap up of what’s new in XaMonkeymarin.

 

Share
Posted in Android, Essentials, iOS, Mobile, Xamarin | Tagged | 1 Comment

Yet Another Podcast #152 – John Papa on Angular 2

John Papa, expert in all things Angular, joins me in discussing Angular 2

 

 

Share
Posted in AngularJS, Bootstrap, Design, Essentials, Learning | Tagged | Leave a comment

Podcast Gold

If you are like me, when you start a podcast, you rarely go back to find old ones, which may be out of date.  However, some of Yet Another Podcast is worth a listen even now.  Here are some of the best that are still relevant.

Share
Posted in Essentials | Leave a comment

Yet Another Podcast #151 – Jon Galloway on //build/

I’m extremely pleased to have Jon back on Yet Another Podcast, this time with a comprehensive review of //build/.

 

 

 

 

Share
Posted in AngularJS, Announcements, ASP.NET, Azure, Community, Essentials, Microsoft, MVC, News, Observations, Visual Studio, Xamarin | Tagged | Leave a comment

Yet Another Podcast #150 – Dan Hermes & Xamarin

Very pleased to have Dan Hermes, author of Xamarin Mobile Application Development, and all-round Xamarin expert.

Share
Posted in Xamarin | Tagged | 1 Comment