Archive for February 16th, 2011

Wednesday, February 16th, 2011

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);

}

Category: .Net, Silverlight | Tags: , ,  | One Comment