【发布时间】:2019-04-09 19:36:44
【问题描述】:
我在 UWP 应用中使用 Prism。我在
中为每个视图模型注册了一些启动参数 protected override async void ConfigureContainer()
我添加了 async 关键字,因为我想初始化一些在 ConfigureContainer() 中等待的数据库连接。但是现在我注意到应用程序启动(有时)没有启动 ags 被 instatiated 导致 null ref 异常。我不应该在这种方法中初始化任何连接吗?为什么应用程序不在 ConfigureContainer() 上等待?应用程序启动时,我应该将异步初始化方法调用放在哪里?方法如下。
protected override async void ConfigureContainer()
{
// register a singleton using Container.RegisterType<IInterface, Type>(new ContainerControlledLifetimeManager());
base.ConfigureContainer();
Container.RegisterInstance<IResourceLoader>(new ResourceLoaderAdapter(new ResourceLoader()));
DocumentClient client = new DocumentClient(new Uri("https://docdb.etc/"),
"my key", new ConnectionPolicy() { ConnectionMode = ConnectionMode.Direct });
try
{
await client.OpenAsync();
}
catch (Exception ex)
{
throw new Exception("DocumentClient client could not open");
}
IDataAccessBM _db = new DataAccessDocDb(client, "ct", "ops");
AddressSearch addresSearcher = new AddressSearch(_db, 4);
StartUpArgs startUpArgs = new StartUpArgs
{
postCodeApiKey = "anotherKey",
db = _db,
fid = "bridge cars",
dialogService = new DialogService(),
addressSearcher = addresSearcher
};
startUpArgs.zoneSet = await _db.ZoneSetGetActiveAsync("another key");
Container.RegisterInstance(startUpArgs);
}
【问题讨论】: