Windows Template Studio — Extending MVVM Basic with Messaging

Scott Kuhl
3 min readDec 6, 2019

--

In my previous article I walked through adding dependency injection and simple inversion of control to the MVVM Basic design pattern added by Windows Template Studio.

In this article we will add a simple messaging system that allows a view model to throw an event that any other view model can catch.

When Do We Need Messaging?

Often when writing an application you will find a flow of logic that naturally fits into one view model calling another view model. Sometimes this logic can start to be complex like when processing an order. And sometimes you just want to make sure your user interface stays in sync when you have two windows showing the same data.

Rather than having all these view models tied together in a workflow style pattern you can use an event based system. When a view model does something important it throws an event that it happened. Other view models that care about that event can then execute code by listening for it.

The Messenger Class

We are going to create one simple class to handle this message pattern. Add the Messenger class to the Helpers folder in the core project from previous article. We are putting it in the core project so this message system could be used between models as well as view models. If you want to limit your events to view models, put it in the Helpers folder in the application project instead.

using System;

namespace MvvmBasicPlus.Core.Helpers
{
public class Messenger<TEvent> where TEvent : EventArgs
{
private static Messenger<TEvent> _m;

public static Messenger<TEvent> Instance => _m ?? (_m = new Messenger<TEvent>());

private Messenger()
{
}

public event Action<object, TEvent> Event;

public void Publish(object source, TEvent ev)
{
Event?.Invoke(source, ev);
}
}
}

This generic class creates a publish and subscribe relationship between two different methods and passes an class with parameters between them.

Let’s look at a example. Create a new folder called Events in the core project and add a class called SampleOrderViewEvent.

using System;

namespace MvvmBasicPlus.Core.Events
{
public class SampleOrderViewEvent : EventArgs
{
public string Name { get; set; }
}
}

The idea behind this event is that every time code is called to view a sample order, the event would be thrown. Contrived yes, but it works for an example.

To keep things simple, we are going to throw the event and listen for it in the same class. This will make it easy for you to make sure it is working. You can always listen for the events in other classes, you just need one active instance of it up and running.

public class ContentGridDetailViewModel : Observable, IDisposable
{
private ISampleDataService _sampleDataService;
private SampleOrder _item;

public SampleOrder Item
{
get { return _item; }
set
{
Set(ref _item, value);
Messenger<SampleOrderViewEvent>.Instance.Publish(this, new SampleOrderViewEvent { Name = _item.ToString() });
}
}

public ContentGridDetailViewModel(ISampleDataService sampleDataService)
{
_sampleDataService = sampleDataService;
private void DisplayMessage(object sender, SampleOrderViewEvent info)
{
Console.WriteLine($"Sample Order {info.Name} event thrown.");
}
public void Dispose()
{
Messenger<SampleOrderViewEvent>.Instance.Event -= DisplayMessage;
}

}
  1. In the constructor we setup a listener for the event and call the display message function when it happens.
  2. When an item is viewed, we trigger the event.
  3. We also made the class disposable so it will stop listening for events after it is unloaded.

That’s all there really is to a simple messaging system!

By adding dependency injection, inversion of control and now messaging, we took a very basic MVVM pattern provided by Windows Template Studio and made it much more flexible without taking on the dependency of a third party framework. I would say at this point we have a minimal but viable MVVM implementation for UWP apps.

A complete working example of this project can be found here on GitHub.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response