据我所知,目前PRISM允许传递字符串,但不允许传递对象。我想知道如何解决这个问题。
我想要传递一个列表集合。在我的情况下,UriQuery没有用处,那我应该怎么办?
Prism 5和6:现在可以使用NavigationParameters类通过区域或RegionManager实例的RequestNavigate方法的重载传递对象参数。
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;
}
}
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);
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);
}
}
就是这样。
您可以使用“object” getter/setter创建PRISM事件。在事件中使用已转换或未转换为“object”的对象引发事件(取决于事件实现是否像著名的“基础设施”项目一样“共享”),然后导航到区域。在实现区域的ViewModel中,订阅上述事件,接收并在本地存储,然后等待“OnNavigatedTo”函数调用。当调用OnNavigatedTo函数时,您已经拥有了对象/类/结构,并且可以运行ViewModel。
例如-事件类:
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; }
}
}
调用类
/// <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));
}
还有这个关于'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;
}
如果您正在使用IOC并且想要使用构造函数注入,您还可以查看如何传递对象。