Wednesday, February 16, 2011

Silverlight, PRISM, MVVM, MEF – Part 7

Regions in Prism have been extended to support a more general notion of navigation, based on URIs and an extensible navigation mechanism.
Navigation within a region means that a new view is to be displayed within that region. The view to be displayed is identified via a URI, which, by default, refers to the name of the view to be created. You can programmatically initiate navigation using the RequestNavigate method defined by the INavigateAsync interface.
RegionManager in prism have a RequestNavigate method which allows you to specify the name of the region to be navigated. This convenient method obtains a reference to the specified region and then calls the RequestNavigate method, as shown in the code.
private void OnViewNavigationRequested()
{
    _regionManager.RequestNavigate("MainRegion"
        , new Uri("EmployeeView1", UriKind.Relative)
        , (navigationResult) =>
        {
            if (navigationResult.Result == true) MessageBox.Show("Success");
            else MessageBox.Show("Failed");
        });
}
The views need to be registered with the region for navigation to work.
[Import]
public EmployeeView1 View1 { get; set; }

[Import]
public EmployeeView2 View2 { get; set; }

public void Initialize()
{
    CreateNavigationTabAndRegisterModule();
    RegisterViewsWithRegion();
}

private void RegisterViewsWithRegion()
{
    _regionManager.RegisterViewWithRegion("MainRegion", () => View1);
    _regionManager.RegisterViewWithRegion("MainRegion", () => View2);
}

View and View Model Participation in Navigation
The INavigationAware interface is implemented by the View and ViewModel to participate in the navigation process. The interface defines the methods
public interface INavigationAware
{
     bool IsNavigationTarget(NavigationContext navigationContext);
     void OnNavigatedFrom(NavigationContext navigationContext);
     void OnNavigatedTo(NavigationContext navigationContext);
}
The OnNavigatedFrom method can be implemented to save the state of the currently active view or viewmodel before navigation. Similarly OnNavigatedTo method can be used to find details of the view or viewmodel navigated to.
If you want to confirm the navigation from the view, the IConfirmNavigationRequest interface can be implemented on the View or ViewModel to confirm the navigation request. The navigation can be canceled or confirmed based on the logic implemented in the ConfirmNavigationRequest method.
public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
{
    if (MessageBox.Show("Confirm navigation from this view?", "Confirm navigation", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
        continuationCallback(true);
    else
        continuationCallback(false);
}

3 comments:

Keith said...

Hey Prajeesh,

Great sample. I have been searching the web for a sample that utilizes SL4, Prism and Authentication and this is the best sample yet. I downloaded your sample from CodePlex but it did not implement the Authentication that you discussed in Blog 7. Do you plan in updating the sample to include authentication? I know it has been a while, but anyone using Prism, I think would find the full implementation invaluable!

Thanks for a great example.

Keith

Hari said...

Hi Prajeesh,
I have been following your series PRISM, MVVM, MEF with much gratitude and admiration. I learned a lot from it.
Is there an example that uses this (Prism, MEF and MVVM) but integrated with a Silverlight(4 or 5) Business or Navigation Application. I find integrating the module/view navigation between prism and silverlight frame navigation cumbersome and difficult.
I particularly like and find the login/user authentication features of the silverlight business app useful and would like to retain it, but use the Prism/MEF and MVVM with WCF Ria services for the rest.
I bet if you created a sample tutorial it would be much valued over here.
Warm Regards and much Thanks from Atlanta
Hari Iyer
iyer.hari.k@gmail.com

Hari said...

Hi Prajeesh,
I have been following your series PRISM, MVVM, MEF with much gratitude and admiration. I learned a lot from it.
Is there an example that uses this (Prism, MEF and MVVM) but integrated with a Silverlight(4 or 5) Business or Navigation Application. I find integrating the module/view navigation between prism and silverlight frame navigation cumbersome and difficult.
I particularly like and find the login/user authentication features of the silverlight business app useful and would like to retain it, but use the Prism/MEF and MVVM with WCF Ria services for the rest.
I bet if you created a sample tutorial it would be much valued over here.
Warm Regards and much Thanks from Atlanta
Hari Iyer
iyer.hari.k@gmail.com