Mike
Mike

Reputation: 3284

Resolving View and ViewModel using IOC

I have a user control implementing the following interface:

public interface IView
    {
        object DataContext { get; set; }
    }

..and it's corresponding view model implementing the following interface:

 public interface ICertificationViewModel
    {     
        string NumOfCertification { get; set; }
    }

I have another service called a NavigationService implementing the following interface:

 public interface INavigationService<TView,TViewModel>
    {
        void ShowView<TView,TViewModel,T>(T model) where T:class;
    }

I'm using unity and I need to put together a new(transient) view and viewmodel whenever the ShowView method on navigation service is called. I cannot inject the View and ViewModel as constructor dependencies(since new instances should be created), and I don't want to take the service locator route(i.e calling unity inside ShowView to resolve the view and viewmodel). Is there a way I can solve this issue using unity or anything else? I have searched everywhere and I cannot find a definite answer.I'm using Prism and .NET 3.5. I would also like to keep this a bit generic so any views and view models could be resolved using the NavigationService ShowView method.

Can you please help with a solution?

Upvotes: 1

Views: 1293

Answers (1)

GOstrowsky
GOstrowsky

Reputation: 989

The Prism library provides a RegionNavigationService that controls Navigation among Views. RegionNavigationService implements IRegionNavigationService and a RequestNavigate() method is defined. You can resolve Navigation for any View registered in the specified Region (It wouldn't make sense to Navigate through Views in different Regions).

/// <summary>
/// Provides navigation for regions.
/// </summary>
public interface IRegionNavigationService : INavigateAsync
{
    ...
}

INavigateAsync:

/// <summary>
/// Provides methods to perform navigation.
/// </summary>
/// <remarks>
/// Convenience overloads for the methods in this interface can be found as extension methods on the 
/// <see cref="NavigationAsyncExtensions"/> class.
/// </remarks>
public interface INavigateAsync
{
    /// <summary>
    /// Initiates navigation to the target specified by the <see cref="Uri"/>.
    /// </summary>
    /// <param name="target">The navigation target</param>
    /// <param name="navigationCallback">The callback executed when the navigation request is completed.</param>
    /// <remarks>
    /// Convenience overloads for this method can be found as extension methods on the 
    /// <see cref="NavigationAsyncExtensions"/> class.
    /// </remarks>
    void RequestNavigate(Uri target, Action<NavigationResult> navigationCallback);
}

If you have some custom pre and post Navigation work done in the ShowView() method, you can implement INavigationAware, which defines OnNavigatedTo() and OnNavigatedFrom(), on the Views or ViewModels involved.

To see how these methods work, the following QuickStart uses OnNavigatedTo() method in order to read and set any context parameters on the new navigated View:

INavigationAware documentation:

If this doesn't help, it would be useful to know the expected behavior the ShowView() method would have.

Regards.

Upvotes: 1

Related Questions