Dinsdale
Dinsdale

Reputation: 641

Using Serial Port Class Library in Prism MVVM Application

I inherited a WPF/Prism/Unity MVVM application and I need to link in a Class Library that communicates externally over a serial port. The serial port library publishes Events for errors and messages.

I'm new to Prism, but I've used Unity some years back. The Prism application (lets call it PrismApp) is a base PrismApplication, with two modules: main and settings. My serial port library (lets call it LibSerial) wraps the base communications protocol and publishes three events: ConnectionEventReceived, ErrorEvent and MessageReceived. LibSerial has functions for Connect, StartSession and Send.

My questions are:

Thanks Everyone!

Upvotes: 0

Views: 355

Answers (1)

mm8
mm8

Reputation: 169200

Where do I instantiate my LibSerial?

Register it in your bootstrapper and let the container instantiate it. Override the RegisterTypes method of the PrismApplication class in your App.xaml.cs and register the LibSerial type:

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    containerRegistry.RegisterSingleton<ILibSerial, LibSerial>();
}

You can then inject your view models with an ILibSerial (which in this case is an interface that the LibSerial class implements) and hook up its events and access its members as usual:

public class ViewModel
{
    public ViewModel(ILibSerial libSerial)
    {
        libSeriel.MessageReceived += ...;
    }
}

The container will take care of the instantiation and provided that you register the type using the RegisterSingleton method in the bootstrapper, there will only be a single instance created and shared across all view models.

Upvotes: 1

Related Questions