问题描述
当尝试构建一个eventaggregator来将我的object
的属性从MainViewModel
转移到SectionViewModel
时,我尝试了以下多篇文章( 1 , 2 , 3 和 4 ),以了解其工作原理,但我似乎缺少了一个步骤. >
在MainViewModel
我有;
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);
}
}
,并且在SectionViewModel
我有;
protected readonly IEventAggregator eventAggregator;
public SectionViewModel(ISectionViewModel sectionViewModel, IEventAggregator eventAggregator)
{
this.eventAggregator = eventAggregator;;
this.eventAggregator.GetEvent<SetObjectEvent<Object>>().Subscribe((object) => { this.Object = object; });
}
谁都抛出自变量The type or namespace name 'SetObjectEvent<>' could not be found
.我以为这是因为我需要一个全局EventAggregator,但有人指出情况并非如此.
这是大多数文章的内容,(至少对我而言)看起来没有引起任何问题.对我来说,这一切似乎都很合乎逻辑,而且我知道应该怎么做.但是,似乎没有任何文章实际上在定义'SetObjectEvent'Event
?而是创建某种通用的EventAggregate
就是这样.那是我对正在发生的事情失去了解的地方.对我来说,听起来很合逻辑,我需要有一个类似事件"的类,它可以容纳项目中所有不同的可调用事件,但是我不知道如何完成此工作?
编辑;正如此答案暗示我认为错不是问题,我改写我的问题以更准确地描述(?)我的问题.
编辑;根据要求SetObjectEvent类
public class SetObjectEvent<TRequest> : PubSubEvent<SetObjectEvent<TRequest>> where TRequest : IObject {}
源自这篇SO文章
编辑;将其更改为
public class SetObjectEvent : PubSubEvent<Object> {}
如在多条注释中所建议的
在GetEvent<SetObjectEvent>()
和GetEvent<SetObjectEvent<Object>>()
事实证明,问题出在何处(还有一点如何*)
我将SetObjectEvent放置在models指令中,因为我认为它是Event的模型.我假设可以在MainViewModel
名称空间中使用using MyProject.MyModule.Models;
正确引用它.原来,我在浏览有关EventAggragator的更多SO文章时错了. SetObjectEvent
应该作为namespace MyProject.MyModule.SetObjectEvent
坐在模块名称空间中.
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>>()
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
.
这篇关于GetEvent< SetObjectEvent>找不到(缺少指令/参考?)-我在做什么错..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!