如何改进在 PRISM 4 中导航到新视图时传递对象
我将 Prism 与 IoC 一起使用。问题是通过导航传递对象(如集合)。我正在看这篇文章: 如何在 PRISM 4 中导航到新视图时传递对象
这是
我提取对象的哈希代码并将其与哈希一起保存在字典
中 的解决方案代码作为密钥和对象作为该对的值。
然后,我将哈希代码附加到 UriQuery
。
之后,我只需获取来自目标视图上的 Uri 的哈希码,并使用它从 Dictionary
请求原始对象。
一些示例代码:
参数存储库类:
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);
}
}
就是这样。
我不确定这个解决方案是否是传递对象的最佳解决方案。我想这可能是一项服务。有一个好的方法可以做到这一点或者有更好的方法吗?
I'm using Prism with IoC. The problem is to pass an object (like collections) through navigation. I was watching this post: How to Pass an object when navigating to a new view in PRISM 4
And this is the solution
I extract the hash code of the object and save it in a Dictionary
, with the hash code as the key and the object as the value of the pair.
Then, I attach the hash code to the UriQuery
.
After, I only have to get the hash code that comes from the Uri on the target view and use it to request the original object from the Dictionary
.
Some example code:
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;
}
}
The 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);
The 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);
}
}
That's it.
I'm not sure if this solution is the best to pass objects. I guess this maybe would be a service. Is a good way to do this or is there a better way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发布了一个更简单的方法。在此提及以供参考 -
我将使用 OnNavieratedTo 和 OnNavieratedFrom 方法来使用 NavigationContext 传递对象。
首先从 INavigationAware 接口派生视图模型 -
然后您可以实现 OnNavieratedFrom 并将要传递的对象设置为导航上下文,如下所示 -
当您想要接收数据时,在第二个视图模型中添加以下代码 -
I posted an easier way. Mentioning it here for reference -
I would use the OnNavigatedTo and OnNavigatedFrom methods to pass on the objects using the NavigationContext.
First derive the viewmodel from INavigationAware interface -
You can then implement OnNavigatedFrom and set the object you want to pass as navigation context as follows -
and when you want to receive the data, add the following piece of code in the second view model -
我刚刚开始使用 Prism,这是我遇到的第一个限制。我用不同的方式解决了它。我首先创建了一个继承自 Uri 并实现 IDictionary 的类(加上一些通用方法以便于访问),
然后我创建了一个继承自 RegionNavigationContentLoader 的类。在 GetContractFromNavigationContext 上,我存储传入的 Uri,以便可以在 CreateNewRegionItem 方法中访问它。在该方法中,我检查 Uri 是否是 NavigationUri,如果是,我会循环添加所有依赖项注入覆盖。我正在使用 Unity,但我认为代码可以轻松转换为另一个 IOC 容器。
现在,在 prism Bootstrapper 中,您需要在 ConfigureServiceLocator 方法中将 BaseRegionNavigationContentLoader 注册为 IRegionNavigationContentLoader。 确保将其标记为 TransientLifetimeManager,以便它每次都会更新。 IRegionNavigationContentLoader 的默认注册是由容器控制的,这使得它的行为就像一个单例,但我们每次都需要一个新的注册,因为我们需要将 uri 从一个方法传递到属性中的下一个方法。
现在我可以编写如下代码,并且仍然使用构造函数注入。
I just started using Prism and this is one of the first limitations I ran into. I solved it a different way. I first created a class that inherits from Uri and implements IDictionary (plus some generic methods for easier access)
Then I created a class that inherits from RegionNavigationContentLoader. On GetContractFromNavigationContext I store the passed in Uri so I can access it in the CreateNewRegionItem method. In that method I check to see if the Uri is the NavigationUri and if so I loop though adding all the dependency injection overrides. I'm using Unity but I assume the code could easy be converted to another IOC container.
Now in the prism Bootstrapper you need to register the BaseRegionNavigationContentLoader as IRegionNavigationContentLoader in the ConfigureServiceLocator method. Make sure you mark it as TransientLifetimeManager so it gets newed up each time. The default registration for IRegionNavigationContentLoader is container controlled which makes it act like a singleton but we need a new one each time since we need to pass the uri from one method to the next in a property.
Now I can wright code like the following and still use constructor injection.