Vince G.
Vince G.

Reputation: 101

Is it possible to use a common view model between a Prism WPF project and Prism Forms (particularly with navigation)?

I'm developing a cross platform app and am using Prism Forms for some of the platforms and Prism WPF for Windows. I would like to use a common view model between the two, but I'm running into challenges with the base classes that the view models derive from, particularly with navigation support.

The samples I've seen for Prism Forms implement INavigationAware. It appears that Prism WPF supports this as well. However, I cannot reference both Prism WPF and Prism Forms from the same project for the obvious reason that there's ambiguous references for the types that are implemented in both.

I was able to abstract a basic navigation interface away for navigating between pages, but I'm not sure how to create a common implementation of INavigationAware that would work correctly both in the Prism Forms and Prism WPF project.

Upvotes: 0

Views: 126

Answers (1)

Vince G.
Vince G.

Reputation: 101

After browsing the source for Prism.Core, I found a suitable workaround: IActiveAware (see reference below)

/// <summary>
/// Interface that defines if the object instance is active
/// and notifies when the activity changes.
/// </summary>
public interface IActiveAware
{
    /// <summary>
    /// Gets or sets a value indicating whether the object is active.
    /// </summary>
    /// <value><see langword="true" /> if the object is active; otherwise <see langword="false" />.</value>
    bool IsActive { get; set; }

    /// <summary>
    /// Notifies that the value for <see cref="IsActive"/> property has changed.
    /// </summary>
    event EventHandler IsActiveChanged;
}

Since INavigationAware is an interface that would need to be implemented by the view model, the shared view model component would need to reference both Prism.Forms and Prism.WPF. This doesn't work because it causes ambiguous references to APIs that are implemented in both.

The original goal was for the view model to receive notification when it was navigated to, or from. IActiveAware addresses this concern, and, because it's implemented in Prism.Core, can be used from a shared view model.

I confirmed on Prism 8 that this interface works as advertised on both Prism Forms and Prism WPF.

Upvotes: 0

Related Questions