【问题标题】:Synchronized message passing with Prism与 Prism 同步消息传递
【发布时间】:2011-11-07 14:31:42
【问题描述】:

在我们的 prism 应用程序中,当用户单击树中的一个项目(单独的模块)时,我们需要将一个模块加载到中心窗格。如果给定路径,中心窗格中的模块(例如设计器模块)可以打开文件并显示自身。如何将文件的路径传递给这个模块? 例如

在设计器模块中

public DesignerViewModel(DataAccess dataAccess)// This will be injected
{
}


//The following class can create the model objects using the IDataService for getting data from remote location
public DataAccess(IDataService service)//this will be injected 
{
}

数据访问对象是 Designer 模块的本地对象,所以我不想将它暴露给模块外部。所以注册是在模块中完成的

public class DesignerModule : IModule
 {
       public void Initialize()
        {
            var container = ServiceLocator.Current.GetInstance<IUnityContainer>();
            container.RegisterType<Object, DesignerView>("DesignerUI");
            container.RegisterType<DataAccess>();             
        }
   }

IDataService在应用层注册

  public class BootStrapper : UnityBootstrapper
  { 
       protected override void ConfigureContainer()
       {
            base.ConfigureContainer();
            Container.RegisterType<IDataService, DataService>();
       }
  }

请注意,IDataService 是整个应用程序的单例。我无法传递特定于 IDataService 中模块实例的文件路径。请注意,您可以根据需要在中心窗格中打开任意数量的模块,只需单击树项->树将触发具有选定项 id 的事件->应用程序将找到与项 id 对应的路径并调用模块。

当我说 _regionManager.AddToRegion("CenterRegion", DesignerModule); 时我将如何传递路径Prism 会把所有的依赖注入都做的很好,但是如何通过路径是个大问题?

【问题讨论】:

    标签: c# unity-container prism


    【解决方案1】:

    您可以使用EventAggregator 与 beetwen 模块交换消息。 每个模块订阅 EventAggregator。 当您打开一个模块时,您可以向主机发送有关新生儿的控制通知。 主机控制用初始化数据发回响应。

    public class MessageEvent : CompositePresentationEvent<Message>{}
    
    internal class MessageReceiver
    {
        private readonly MessageEvent _evt;
        private readonly string _myId = Guid.NewGuid().ToString();
    
        internal MessageReceiver(IEventAggregator eventAggregator)
        {
            _evt = eventAggregator.GetEvent<MessageEvent>();
            _evt.Subscribe(Receive, true);
    
            _evt.Publish(new Message { Source = _myId, Command = Message.Commands.WhoIAm });
        }
        public void Receive(Message message)
        {
            switch (message.Command)
            {
                case Message.Commands.WhoIAm:
                    _evt.Publish(
                        new Message
                        {
                            Destination = message.Source,
                            Command = Message.Commands.Initialize,
                            MessageData = Encoding.UTF8.GetBytes("init data")
                        });
                    break;
                case Message.Commands.Initialize:
                    if (message.Destination == _myId)
                    {
                        //init
                    }
                    break;
            }
        }
    }
    public class Message
    {
        public enum Commands { Initialize, WhoIAm }
    
        public string Source { get; set; }
        public string Destination { get; set; }
        public Commands Command { get; set; }
        public byte[] MessageData { get; set; }
    }
    

    【讨论】:

    • @Radik-这不是我想要的。请注意,模块创建本身是因为来自包含路径信息的树模块的事件。此事件将由主应用程序处理,主应用程序将在应用程序的中心窗格中创建一个设计器模块。我更喜欢在创建模块实例时传递路径信息。
    【解决方案2】:

    我从我的同事那里找到了答案。调用 Resolve() 时,可以用自己的对象覆盖参数。因此,创建将要注入的对象,填充它并使用带有 ParameterOverride 的 Resolve() 传递。在 google 中搜索 ParameterOverride 以获取更多信息。

    【讨论】:

      猜你喜欢
      • 2011-08-27
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      • 2013-08-24
      • 1970-01-01
      • 1970-01-01
      • 2014-04-01
      • 2015-01-10
      相关资源
      最近更新 更多