我正在尝试将Event Aggregator集成到我的应用程序中,并遵循在线文档,但我还无法使其完全正常运行,也无法找到完整的示例进行查看。我尝试构建的应用程序是一个用于我们内部知识库的简单搜索引擎,因此它只有3个视图-主搜索页面(根页面),结果页面和详细信息页面。
下面,我刚刚添加了我认为到目前为止调试该错误的相关代码,我正在构建该错误。如果您需要更多片段,则非常乐意提供!
诚然,我真的不知道引导程序是如何工作的-到目前为止一直在遵循教程和文档。
我的代码如下:
Bootstrapper.cs
namespace CleverBot
{
class Bootstrapper : BootstrapperBase
{
private readonly SimpleContainer _container = new SimpleContainer();
public Bootstrapper()
{
Initialize();
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
base.OnStartup(sender, e);
DisplayRootViewFor<ShellViewModel>();
}
protected override void Configure()
{
_container.Singleton<IEventAggregator, EventAggregator>();
_container.RegisterPerRequest(typeof(DetailedDocumentViewModel), null, typeof(DetailedDocumentViewModel));
_container.RegisterPerRequest(typeof(SearchPageViewModel), null, typeof(SearchPageViewModel));
_container.RegisterPerRequest(typeof(SearchResultsViewModel), null, typeof(SearchResultsViewModel));
_container.RegisterPerRequest(typeof(ShellViewModel), null, typeof(ShellViewModel));
}
protected override object GetInstance(Type serviceType, string key)
{
return _container.GetInstance(serviceType, key);
}
protected override IEnumerable<object> GetAllInstances(Type serviceType)
{
return _container.GetAllInstances(serviceType);
}
protected override void BuildUp(object instance)
{
_container.BuildUp(instance);
}
}
}
而且我的ShellViewModel.cs看起来像这样:
namespace CleverBot.ViewModels
{
public class ShellViewModel : Conductor<object>
{
private readonly IEventAggregator _eventAggregator;
public ShellViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
Startup.Init<SolrModel>("http://localhost:8983/solr/brain_drive");
ShowSearchPage();
}
public void ShowSearchPage()
{
ActivateItem(new SearchPageViewModel(_eventAggregator));
}
}
}
最后,我的SearchPageViewModel.cs看起来像:
namespace CleverBot.ViewModels
{
public class SearchPageViewModel : PropertyChangedBase
{
private string _searchTerm;
private readonly IEventAggregator _eventAggregator;
public SearchPageViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
}
public string SearchTerm
{
get { return _searchTerm; }
set
{
_searchTerm = value;
NotifyOfPropertyChange(() => SearchTerm);
}
}
public void BasicSearchButton()
{
// Build the new query object
var queryResult = new SolrQuery(_searchTerm);
// Execute the query, while also applying all the options
var Result = ExecuteQuery(queryResult);
// Publish the result, to be picked up by SearchResults VM
_eventAggregator.PublishOnUIThread(Result);
}
private SolrQueryResults<SolrModel> ExecuteQuery(SolrQuery query)
{
var solr = CommonServiceLocator.ServiceLocator.Current.GetInstance<ISolrOperations<SolrModel>>();
QueryOptions options = getQueryOptions();
var docs = solr.Query(query, options);
return docs;
}
private QueryOptions getQueryOptions()
{
var facetPivotQuery = new SolrFacetPivotQuery()
{
Fields = new[] { new PivotFields("created_year", "created_month") },
MinCount = 1
};
QueryOptions options = new QueryOptions
{
Facet = new FacetParameters
{
Queries = new ISolrFacetQuery[]
{
new SolrFacetFieldQuery("type"),
new SolrFacetFieldQuery("source"),
facetPivotQuery
}
},
Highlight = new HighlightingParameters
{
Fields = new[] { "text", "id", "author" },
Fragsize = 150,
Snippets = 200,
MaxAnalyzedChars = -1
}
};
return options;
}
}
}
有人能够阐明为什么我遇到System.NullReferenceException错误吗?我知道这是什么意思,但是我不了解如何从此框架开始就得到它。
提前感谢!
看来我必须注册以下内容:
_container.Singleton<IWindowManager, WindowManager>();
在我的bootstrapper.cs中,现在看起来像:
namespace CleverBot
{
class Bootstrapper : BootstrapperBase
{
private SimpleContainer _container = new SimpleContainer();
public Bootstrapper()
{
Initialize();
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
DisplayRootViewFor<ShellViewModel>();
}
protected override void Configure()
{
_container.Singleton<IEventAggregator, EventAggregator>();
// THE FOLLOWING WAS ADDED
_container.Singleton<IWindowManager, WindowManager>();
GetType().Assembly.GetTypes()
.Where(type => type.IsClass)
.Where(type => type.Name.EndsWith("ViewModel"))
.ToList()
.ForEach(viewModelType => _container.RegisterPerRequest(
viewModelType, viewModelType.ToString(), viewModelType));
}
protected override object GetInstance(Type serviceType, string key)
{
return _container.GetInstance(serviceType, key);
}
protected override IEnumerable<object> GetAllInstances(Type serviceType)
{
return _container.GetAllInstances(serviceType);
}
protected override void BuildUp(object instance)
{
_container.BuildUp(instance);
}
}
}