UWP Mvvm Prism ConfirgureContainer 问题
UWP Mvvm Prism ConfirgureContainer Issue
我在 UWP 应用程序中使用 Prism。我正在为
中的每个视图模型注册一些启动参数
protected override async void ConfigureContainer()
我添加了 async 关键字,因为我想初始化一些可在 ConfigureContainer() 中等待的数据库连接。但是现在我注意到应用程序启动时(有时)没有启动 ags 被实例化导致 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);
}
Should I not initialise any connections in this method?
至少不是异步的。我宁愿创建一个 ConnectionFactory
它(可能是异步的)按需创建连接。
Why is it the app not waiting on the ConfigureContainer()?
因为不能await
void
。这就是不鼓励使用 async void
的原因……async Task
中的 Task
是 await
ed,而不是 async
。
Where should I put async initialising method calls when app starts up?
没有 async
构造函数或 async new
这样的东西。 this post by Stephen Cleary.
是探索此处选项的良好开端
Container.RegisterInstance<IResourceLoader>(new ResourceLoaderAdapter(new ResourceLoader()));
注册实例很丑陋,而且大多数时候都是不必要的(这是一个例子)。如果你重构你的代码让容器完成它的工作,你的异步初始化问题就会消失。
不要把你的初始化代码放在
protected override void ConfigureContainer()
放入:
protected override async Task OnInitializeAsync(IActivatedEventArgs args)
可以从那里访问容器,方法是异步的。
我在 UWP 应用程序中使用 Prism。我正在为
中的每个视图模型注册一些启动参数 protected override async void ConfigureContainer()
我添加了 async 关键字,因为我想初始化一些可在 ConfigureContainer() 中等待的数据库连接。但是现在我注意到应用程序启动时(有时)没有启动 ags 被实例化导致 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);
}
Should I not initialise any connections in this method?
至少不是异步的。我宁愿创建一个 ConnectionFactory
它(可能是异步的)按需创建连接。
Why is it the app not waiting on the ConfigureContainer()?
因为不能await
void
。这就是不鼓励使用 async void
的原因……async Task
中的 Task
是 await
ed,而不是 async
。
Where should I put async initialising method calls when app starts up?
没有 async
构造函数或 async new
这样的东西。 this post by Stephen Cleary.
Container.RegisterInstance<IResourceLoader>(new ResourceLoaderAdapter(new ResourceLoader()));
注册实例很丑陋,而且大多数时候都是不必要的(这是一个例子)。如果你重构你的代码让容器完成它的工作,你的异步初始化问题就会消失。
不要把你的初始化代码放在
protected override void ConfigureContainer()
放入:
protected override async Task OnInitializeAsync(IActivatedEventArgs args)
可以从那里访问容器,方法是异步的。