如何在PRISM中导航到新视图时传递对象?

4
据我所知,目前PRISM允许传递字符串,但不允许传递对象。我想知道如何解决这个问题。 我想要传递一个列表集合。在我的情况下,UriQuery没有用处,那我应该怎么办?
4个回答

6

Prism 5和6:现在可以使用NavigationParameters类通过区域或RegionManager实例的RequestNavigate方法的重载传递对象参数。


3
好的,我会尽力完成您的翻译需求。以下是需要翻译的内容:it would be better to add an example or even a link最好添加一个例子或链接 - Hakan Fıstık

2
我有我的技巧。
我提取对象的哈希码并将其保存在一个字典中,哈希码作为键,对象作为配对的值。
然后,我将哈希码附加到UriQuery中。
接下来,我只需要从目标视图中获取来自Uri的哈希码,并使用它从字典中请求原始对象。
以下是一些示例代码:
参数仓库类:
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);
    }
}

就是这样。


只有一件事,你认为...参数作为静态类还是不作为服务更好? - Darf Zon
好的,那是我在尝试实现功能时做的一个变通方法。欢迎任何改进意见。如果您改进了这个示例,请在这里分享。 ;) 再见 - Diego Stiehl
1
由于您将实例放在参数“cache”中,然后传递“key”,因此您可能希望只创建一个新的Guid(它是唯一的,而哈希不是),并在检索到对象后释放缓存中的对象。 - Bahri Gungor
正如 @BahriGungor 所述,哈希不是唯一键,也不应该作为唯一键使用。 - Lukazoid

1

您可以使用“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;
     }

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接