Prism 8

De Banane Atomic
Aller à la navigationAller à la recherche

Links

Description

Fully open source version of Prism including MVVM, dependency injection, commands, EventAggregator, and others.
Prism 8 supports WPF, Xamarin Forms and UNO, but not Silverlight, Windows 8/8.1/WP8.1 or UWP.

Getting Started

Dependency Injection

App.xaml.cs
public partial class App
{
    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        // register a singleton service
        containerRegistry.RegisterSingleton<IMyService, MyService>();
        // registera transient service
        containerRegistry.Register<IMyService, MyService>();
    }
singleton service for a service which is used throughout the application and that retains its state.
transient service create a new instance each time.
scoped service no implementation because unlike a web application, desktop applications are dealing with a single user and not scoped user requests.

Commands

MyViewModel.cs
public DelegateCommand DoCommand { get; }
public DelegateCommand DoWithParamCommand<string> { get; }

public MyViewModel()
{
    DoCommand = new DelegateCommand(Do, CanDo);
    DoWithParamCommand = new DelegateCommand<string>(DoWithParam, CanDoWithParam);
}

private void Do() { }
private bool CanDo() => true;

private void DoWithParam(string parameter) { }
private bool CanDoWithParam(string parameter) => true;
MyView.xaml
<Button Command="{Binding DoCommand}"
        Content="Do" />

<Button Command="{Binding DoWithParamCommand}"
        CommandParameter="{Binding Content, RelativeSource={RelativeSource Self}}}"
        Content="Do" />
Parameter can't be of value type (int, double, bool, etc). Use instead the equivalent nullable type.

RaiseCanExecuteChanged

Cs.svg
private bool isEnabled;

public bool IsEnabled
{
    get => isEnabled;
    set
    {
         SetProperty(ref isEnabled, value);
         SubmitCommand.RaiseCanExecuteChanged();
    }
}

ObservesProperty

Whenever the value of the supplied property changes, the DelegateCommand will automatically call RaiseCanExecuteChanged to notify the UI of state changes.

Cs.svg
DoCommand = new DelegateCommand(Do, CanDo).ObservesProperty(() => IsEnabled);

ObservesCanExecute

If your CanExecute is the result of a simple Boolean property, you can eliminate the need to declare a CanExecute delegate, and use the ObservesCanExecute method instead. ObservesCanExecute will not only send notifications to the UI when the registered property value changes but it will also use that same property as the actual CanExecute delegate.

Cs.svg
DoCommand = new DelegateCommand(Do).ObservesCanExecute(() => IsEnabled);

Task-Based DelegateCommand

Cs.svg
DoCommand = new DelegateCommand(DoAsync);

async void DoAsync()
{
    await SomeAsyncMethod();
}

Event Aggregator

Event mechanism that enables communications between loosely coupled components in the application.

MyApp.Shared/MyEvent.cs
public class MyEvent : PubSubEvent<string>
{ }
MyApp/MyViewModel.cs
internal sealed class MyViewModel
{
    IEventAggregator eventAggregator;

    public MyViewModel(IEventAggregator eventAggregator)
    {
        this.eventAggregator = eventAggregator;
    }

    // publishing an event
    eventAggregator.GetEvent<MyEvent>().Publish("KeyMessage");

    // subscribing to events
    eventAggregator.GetEvent<MyEvent>().Subscribe(DisplayMessage);
    void DisplayMessage(string keyMessage)
    { }
}

Subscribing on the UI Thread

If the subscriber needs to update UI elements in response to events, subscribe on the UI thread. In WPF, only a UI thread can update UI elements.

Cs.svg
eventAggregator.GetEvent<MyEvent>().Subscribe(DisplayMessage, ThreadOption.UIThread);
PublisherThread use this setting to receive the event on the publishers' thread. Default value.
UIThread use this setting to receive the event on the UI thread.
BackgroundThread use this setting to asynchronously receive the event on a .NET Framework thread-pool thread.
In order for PubSubEvent to publish to subscribers on the UI thread, the EventAggregator must initially be constructed on the UI thread.

Subscription Filtering

Cs.svg
eventAggregator.GetEvent<MyEvent>().Subscribe(DisplayMessage, ThreadOption.UIThread, false, x => x == "KeyMessage");

Performance concern

Cs.svg
var keepSubscriberReferenceAlive = true;
var myEvent = eventAggregator.GetEvent<MyEvent>();
myEvent.Subscribe(DisplayMessage, keepSubscriberReferenceAlive);

myEvent.Unsubscribe(DisplayMessage);
keepSubscriberReferenceAlive
true the event instance keeps a strong reference to the subscriber instance, thereby not allowing it to get garbage collected. For information about how to unsubscribe, see the section Unsubscribing from an Event later in this topic.
false default value. The event maintains a weak reference to the subscriber instance, thereby allowing the garbage collector to dispose the subscriber instance when there are no other references to it. When the subscriber instance gets collected, the event is automatically unsubscribed.