In prism, when you want to import pages, input parameters such as MyView? Param1 = abc & amp; param2 = 123, How to obtain these parameters? This requires implementing the INavigationAware interface.
The View above is called MyView. If its ViewModel is called MyViewModel, The ViewModel must implement the INavigationAware interface.
INavigationAware Interface source code
View Code
public interface INavigationAware{bool IsNavigationTarget(NavigationContext navigationContext);void OnNavigatedTo(NavigationContext navigationContext);void OnNavigatedFrom(NavigationContext navigationContext);}
These three methods are important:
- IsNavigationTargetMethod: whether the current view model can process the requested navigation behavior is usually used to specify whether the current view/model can be reused. For example, a view that displays customer details can display information such as customer a and Customer B. They reuse the same view.
- OnNavigatedToMethod: when the current page is navigated, this function can be used to process URI parameters.
- OnNavigatedFromMethod: This occurs when you navigate to another page on the current page.
Here is an example of ViewModel that implements the INavigationAware interface:
View Code
1 [Export("MyViewModel ", typeof(MyViewModel ))] 2 [PartCreationPolicy(CreationPolicy.NonShared)] 3 public class MyViewModel : INavigationAware 4 { 5 private IRegionNavigationJournal navigationJournal; 6 7 bool INavigationAware.IsNavigationTarget(NavigationContext navigationContext) 8 { 9 return true;10 }11 12 void INavigationAware.OnNavigatedFrom(NavigationContext navigationContext)13 {14 // Intentionally not implemented.15 }16 17 18 void INavigationAware.OnNavigatedTo(NavigationContext navigationContext)19 {20 var pram1 = navigationContext.Parameters["param1"];21 22 UpdateDataAsync(pram1 );23 24 this.navigationJournal = navigationContext.NavigationService.Journal;25 }26 27 28 29 }
In the preceding example, the OnNavigatedTo method obtains the URI parameter and automatically refresh the ViewModel when the parameter changes. This scenario can be used: for example, a view that displays customer details can display information of Customer a, Customer B... and so on. They reuse the same view. When we navigateURI: MyView? Param1 = abc & amp; param2 = 123The client ID changes in the same view and is passed in the URI parameters. The OnNavigatedTo method obtains the URI parameters, reuse the current view, and refresh the data. The entire process is clear.
IConfirmNavigationRequestInterface
Sometimes, when we navigate to other pages, a dialog box pops up prompting the user to "do you want to discard the modification? Save, discard, cancel ", You need to implement the IConfirmNavigationRequest interface. In the above example, the ViewModel also implements the IConfirmNavigationRequest interface. In this way, this ViewModel implements both the INavigationAware interface and the IConfirmNavigationRequest interface. The trigger sequence is as follows:
- If the IConfirmNavigationRequest interface is also implemented for the target when navigation occurs, the ConfirmNavigationRequest is automatically called first.
- ViewModel triggers interaction to open a "Do You Want To discard modification? Save, discard, and cancel the "OK" UI.
- After the user selects "Save, discard, cancel", the interaction callback is automatically triggered.
- Choose whether to continue Navigation Based on the user's choice.
Here is an example:
View Code
ViewModel implements the intent interface 1 public class ComposeEmailViewModel: icationicationobject, intent {3 private readonly InteractionRequest <Confirmation> 4 confirmitinteractionrequest; 5 public ComposeEmailViewModel (IEmailService emailService) 6 {7 this. confirmExitInteractionRequest = new8 InteractionRequest <Confirmation> (); 9} 10 public IInteractionRequest ConfirmExitInteractionRequest11 {12 get {return this. confirmExitInteractionRequest;} 13} 14} XAML defines Interaction <UserControl. resources> <DataTemplate x: name = "confirmitdialogtemplate"> <TextBlock HorizontalAlignment = "Center" verticalignment = "Center" Text = "{Binding}"/> </DataTemplate> </UserControl. resources> <Grid x: Name = "LayoutRoot" Background = "White"> <ei: Interaction. triggers> <prism: Triggers = "{Binding ConfirmExitInteractionRequest}"> <prism: Triggers = "{StaticResource confirmitdialogtemplate}"/> </prism: InteractionRequestTrigger> </ei: Interaction. triggers>... the user confirms void IConfirmNavigationRequest. confirmNavigationRequest (NavigationContext navigationContext, Action <bool> continuationCallback) {this. confirmExitInteractionRequest. raise (new Confirmation {Content = "... ", Title = "... "}, c => {continuationCallback (c. confirmed );});}