问题
While trying to build an eventaggregator to have the properties of my object
be transfered from MainViewModel
to SectionViewModel
I've tried following multiple articles (1, 2, 3 and 4 ) as to how this works, but I seem to be missing a step..?
In the MainViewModel
I have;
protected readonly IEventAggregator eventAggregator;
public MainViewModel(IMainViewModel mainViewModel, IEventAggregator eventAggregator)
{
...Some other Code...
this.eventAggregator = eventAggegator;
}
public Object Object
{
...Getter...
set
{
this.object = value;
this.RaisePropertyChanged();
this.EventAggregator.GetEvent<SetObjectEvent<Object>>().Publish(this.Object);
}
}
and in the SectionViewModel
I have;
protected readonly IEventAggregator eventAggregator;
public SectionViewModel(ISectionViewModel sectionViewModel, IEventAggregator eventAggregator)
{
this.eventAggregator = eventAggregator;;
this.eventAggregator.GetEvent<SetObjectEvent<Object>>().Subscribe((object) => { this.Object = object; });
}
Whom both throw the argument excpetion The type or namespace name 'SetObjectEvent<>' could not be found
. I thought this was because I needed a global EventAggregator but someone pointed out this is not the case.
This is the giste of most of the articles, which (to me at least) does not look like it's causing any problems. It all seems pretty logical to me, and I understand what it's supposed to do. However, none of the articles seem to actually be defining the 'SetObjectEvent' Event
? Instead the create some sort of general EventAggregate
and that's it. That's where I kind of lose track on what's happening. To me it sounds pretty logical I'll need to have a class something like 'Events' that can hold all the different callable events in the project, but I'd have no idea as to how to accomplish this ?
Edit; As this answer implied that what I think is wrong is not the problem, I rephrased my question to more accurately (?) describe my problem.
Edit; As requested the SetObjectEvent class
public class SetObjectEvent<TRequest> : PubSubEvent<SetObjectEvent<TRequest>> where TRequest : IObject {}
derived from this SO article
Edit; changing it to
public class SetObjectEvent : PubSubEvent<Object> {}
as suggested in multiple comments gives me the same argument exception on both GetEvent<SetObjectEvent>()
and GetEvent<SetObjectEvent<Object>>()
回答1:
As turns out, the problem was where (and just a little bit how*)
I had the SetObjectEvent placed in the models directive because I figured it was a model for the Event. I assumed it'd be referenced properly with using MyProject.MyModule.Models;
within the MainViewModel
namespace. Turns out I was wrong while browsing some more SO articles regarding the EventAggragator. The SetObjectEvent
should be sitting in the module namespace as namespace MyProject.MyModule.SetObjectEvent
.
*Seemingly it should be public class SetObjectEvent: PubSubEvent {} – dvorn
回答2:
The EventAggregator class does all this for you. Internally, it has "events" dictionary
来源:https://stackoverflow.com/questions/39446466/geteventsetobjectevent-could-not-be-found-missing-directive-reference-wha