How to transfer an object when navigating to a new view in PRISM?

As far as I know, PRISM currently allows the transfer of strings, but does not allow the transfer of objects. I would like to know what are the ways to overcome this problem.

I want to pass a collection of lists. In my case, UriQuery is not useful, what should I do in this case?

+4
source share
4 answers

I have my own technique.

I extract the hash code of the object and save it in the Dictionary , and the hash code as the key, and the object as the value of the pair.

Then I attach the hash code to UriQuery .

After that, I need to get the hash code that comes from Uri in the target view, and use it to query the source object from Dictionary .

Code example:

Parameter Repository Class:

 public class Parameters { private static Dictionary<int, object> paramList = new Dictionary<int, object>(); public static void save(int hash, object value) { if (!paramList.ContainsKey(hash)) paramList.Add(hash, value); } public static object request(int hash) { return ((KeyValuePair<int, object>)paramList. Where(x => x.Key == hash).FirstOrDefault()).Value; } } 

Caller Code:

 UriQuery q = null; Customer customer = new Customer(); q = new UriQuery(); Parameters.save(customer.GetHashCode(), customer); q.Add("hash", customer.GetHashCode().ToString()); Uri viewUri = new Uri("MyView" + q.ToString(), UriKind.Relative); regionManager.RequestNavigate(region, viewUri); 

Target View Code:

 public partial class MyView : UserControl, INavigationAware { // some hidden code public void OnNavigatedTo(NavigationContext navigationContext) { int hash = int.Parse(navigationContext.Parameters["hash"]); Customer cust = (Customer)Parameters.request(hash); } } 

What is it.

+3
source

Prism 5 and 6: Now the NavigationParameters class can be used to pass object parameters during navigation using the RequestNavigate method overloads for an instance of Region or RegionManager.

+2
source

You can create a PRISM event using the getter / setter "object". The Rise event with its object cast or not dropped to the "object" inside the event (depends on whether the implementation of the events is implemented "jointly", as in the well-known projects "Infrastructure"), and then "Go to the region". In ViewModel, which implement Region - Subscribe () before the event above, receive it and store it locally, and then just wait for the OnNavigatedTo function to be called. When the OnNavigatedTo function is called, you already have an object / class / struct and can run ViewModel.

For example - Event Class:

 namespace CardManagment.Infrastructure.Events { using Microsoft.Practices.Prism.Events; /// <summary> /// Event to pass 'Selected Project' in between pages /// </summary> public class SelectedProjectViewEvent : CompositePresentationEvent<SelectedProjectViewEvent> { public object SelectedPorject { get; set; } } } 

Class call

 /// <summary> /// Called when [back to project view]. /// </summary> /// <param name="e">The e.</param> public void OnBackToProjectView(CancelEditProjectEvent e) { eventAggregator.GetEvent<SelectedProjectViewEvent>().Publish(new SelectedProjectViewEvent() { SelectedPorject = selectedProject }); regionManager.RequestNavigate(WellKnownRegionNames.ProjectViewRegion, new System.Uri("ProjectDetailsView", System.UriKind.Relative)); } 

And this is in the class "Receiver"

  /// <summary> /// Called when the implementer has been navigated to. /// </summary> /// <param name="navigationContext">The navigation context.</param> public void OnNavigatedTo(NavigationContext navigationContext) { if (this.SelectedProject == null) // <-- If event received untill now this.ShouldBeVisible = false; else this.ShouldBeVisible = true; } 
0
source

You can also check how to pass objects if you use IOC and want to use constructor injection.

fooobar.com/questions/1396947 / ...

0
source

Source: https://habr.com/ru/post/1396945/


All Articles