开发者

Prism MVVM - How to pass an IEventAggregator to my ViewModel

recently I started working with Prism in Silverlight. I want to use the EventAggregator to Subscribe and Publish events between two ViewModels. As I saw on some guides, the ViewModel's ctor should accept IEventAggregator as a parameter. I can't find out how to do this hence my View always wants to initialize the ViewModel with a parameterless ctor.

My ViewModel ctor:

MyViewModel(IEventAggregator eventAggregator)
{
    // get the event....
}

My View:

<UserControl ....>

    <UserControl.Resources>
        <ViewModels:MyViewModel x:Key="MyViewModel"/>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource MyViewModel}}">
    ....
    <Grid/>

</UserControl>

I can instantiate the ViewModel in the ctor of the View, and then assign it to its DataContext, but then I must have an IEventAggregator in my View, which I also cannot get. but this is probably not the correct开发者_Python百科 way to pass an IEventAggregator (or any other object! - IUnityContainer for example) to the ViewModel.

Can someone tell me what I'm doing wrong?


You have to resolve your dependency via unity. Have a look at the prism MVVM examples and the ui composition. There the view does not create the view model, but it is exactly the other way round. The view model gets the view injected via constructor injection. The view model sets itself as view model for the view:

public interface IView
{
    IViewModel ViewModel{get;set;}
}

public interface IViewModel { }

public View:UserControl, IView
{
    public IViewModel ViewModel
    {
        get{return DataContext as IViewModel;}
        set{DataContext = value;}
    }
}

public ViewModel:IViewModel
{
    public ViewModel(IView view, IEventAggregator eventAggregator)
    {
        view.ViewModel = this;
        //get the event...
    }
}

Using this approach you have to register the view model and the view to unity. Afterwards you only have to resolve the view model, the view is injected by the container.

To get the view to the right place on the user interface you have to register the view to a region using the RegionManager. When this is all set up, creating a new view model instance results in adding the view into the registered region so that it shows up on the user interface.


Other than having the ViewModel hook itself into the data context of the view (which I don't like at all), there are two other options that I can think of in Silverlight.

  1. Utilize the ServiceLocator pattern to allow your static resources to create themselves via the container. MVVMLight has a fairly good pattern for this.
  2. Use a framework like Caliburn.Micro, which plugs in a nice set of conventions that will wire up many things based on naming conventions, including bindings and viewmodels.


Maybe you've solved it already but

http://www.emileinarsson.se/silverlight-4-mvvm-prism-unity-dependency-injection/

this post explains how to use Unity in a MVVM environment.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜