Search code examples
c#architecturecontainersprism

What should I do to make a preview window with Prism UnityContainer?


I'm quite new to Prism containers and want to create an app in WPF with a preview window. What should I do to make it in the right way? I made this CreateShell method in my Application class (that inherits from PrismApplication):

protected override Window CreateShell()
{
    var previewWindow = Container.Resolve<IPreviewWindow>();
    previewWindow.PreviewDone += (e, a) =>
    {
        (previewWindow as Window).Close();
        var window = Container.Resolve<IBaseWindow>() as Window;
        window.Show();
    };
    return previewWindow as Window;
}

The IPreviewWindow:

public interface IPreviewWindow : IBaseWindow
{
    event EventHandler<EventArgs> PreviewDone;
}

Is it OK doing that thing this way?


Solution

  • The code you've written for CreateShell method and IPreviewWindow interface seems good for most scenarios, it registers a PreviewDone event to the IPreviewWindow, and when this event is triggered, it closes the preview window and opens the main window (IBaseWindow).

    However, there are a few points I would suggest for better practice and maintainability:

    1. Loosely Coupled Events: Prism provides an EventAggregator for event handling that could be a better way to handle communication between components instead of directly subscribing to events. You can create a PreviewDoneEvent class that inherits from PubSubEvent and use the EventAggregator to subscribe/publish this event. This helps to make your components more decoupled.

    2. Navigations: Instead of manually creating windows and closing them, you might want to consider using Prism's navigation system. With this, you can navigate to different views without caring about which window they should be in, and it makes your application more maintainable.

    3. Single Responsibility Principle: The CreateShell method's main responsibility should be just creating the shell, and it might not be the best place to handle navigation or switching windows. For this, you might want to create a separate service or use Prism's built-in features as mentioned above.

    4. Thread Safety: Keep in mind that in WPF, any operations on the UI elements should be on the UI thread, so make sure the PreviewDone event is invoked on the UI thread.

    Here is an example of how you can rewrite your CreateShell method using EventAggregator and Prism's navigation:

    protected override Window CreateShell()
    {
        Container.Resolve<IEventAggregator>().GetEvent<PreviewDoneEvent>().Subscribe(OnPreviewDone);
        return Container.Resolve<IPreviewWindow>() as Window;
    }
    
    private void OnPreviewDone()
    {
        Application.Current.Dispatcher.Invoke(() =>
        {
            var window = Container.Resolve<IBaseWindow>() as Window;
            window.Show();
        });
    }
    

    This example assumes you have a PreviewDoneEvent that inherits from PubSubEvent and it is published when the preview is done.

    Remember, this is just a basic example. Depending on your requirements and the complexity of your application, you might need to adjust the code accordingly.