开发者

Prism wfh pass entity to modal for modal View

I'm using the MVVM pattern with Prism 2.0 framework and WPF. I've run into a problem with a modal Window and initializing a ViewModel using Events. In my module I have some code that creates an object which I then want to pass this into my ViewModel so that the View can bind to it's properties.

Normally I'd use the EventAggregator to publish an event containing my object that can be be subscribed to in the ViewModel. However in this scenario I'm creating a new modal Window and therefore the ViewModel is not created in time to subscribe to the event before I can publish it. I'm trying to avoid passing the object in to the Window as DataContext or reverting to other mechanisms. Does anyone have a solution to get this working? Maybe some way of forcing the View to load before a call to ShowDialog or Show?

    var popup= new PopUpWindow();
    regionManager.RegisterViewWithRegion("MyRegion", typeof(MyView));
    eventAggregator.GetEvent<NotifyObjectEvent>().Publish(myObject);
    // ViewModel only created and subscribes to event when the line below is run
    popup.ShowDialog();

My hack to make this work is as follows, but I'm wondering if there's a more elegant solution I'm missing?

    var popup= new PopUpWindow();
    regionManager.RegisterViewWithRegion("MyRegion", typeof(MyView));
    popup.Show();
    popup.Hide();
    eventAggregator.GetEvent<NotifyObjectEvent>()开发者_如何学Go.Publish(myObject);
    popup.ShowDialog();

Ok maybe I've figured it out, seems to work at least...

    var popup= new PopUpWindow();
    regionManager.RegisterViewWithRegion("MyRegion", typeof(MyView));
    RegionManager.SetRegionManager(popup, regionManager);
    regionManager.AddToRegion("MyRegion", typeof(MyView));
    eventAggregator.GetEvent<NotifyObjectEvent>().Publish(myObject);
    popup.ShowDialog();


You could use something similar to Ade Miller's cached event aggregator. This link is from '08, but it should still be useful: http://www.ademiller.com/blogs/tech/2008/11/adding-store-and-forward-support-to-the-prism-eventaggregator/

The idea is to publish the event and in case there are no subscribers, store it until the first subscriber appears .

I hope this helps.

Thanks, Damian

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜