将caliburn.micro做与视图模型异步方法正确的事情? [英] Will caliburn.micro do the right thing with async method on ViewModel?
问题描述
至于其他地方提到,新的.NET异步/待机模式通过传播像一个病毒的软件层。最近异步变化现在已经冒泡到我的视图模型,我想知道,如果它是从公共无效DoStuff()
到公共安全变化宣言异步任务DoStuff()
?
As mentioned elsewhere, the new .NET async/await model propagates through layers of software like a virus. A recent async change has now bubbled up to my view model, and I am wondering if it is safe change declaration from public void DoStuff()
to public async Task DoStuff()
?
谢谢!
推荐答案
支持的Caliburn.Micro异步编程模型现在是pretty不错。
The support of asynchronous programming model in Caliburn.Micro is pretty good now.
几件事情可以做:
- 使用异步/等候在行动方法。要小心,因为操作方法在技术上事件处理程序,你务必做好
异步无效
,而不是异步任务
。 - 的异步事件处理程序屏幕的事件,像活性炭,ViewLoaded等。
- 异步将覆盖屏幕的方法:OnInitialize,OnActivate,...您可以覆盖然后根据
保护覆盖异步无效OnInitialize(){}
和里面你可以等待另一项任务。 - 转换到协同程序任务。使用
ExecuteAsync()
扩展方法。协同程序还是有一定的优势,在某些情况下,如执行上下文。 -
IHandleWithTask< TMessage>
- pretty方便...
- Use async/await in Action method. Be careful, as action methods are technically event handlers, you shoud do
async void
rather thanasync Task
. - Asynchronous event handlers for Screen's events, like Activated, ViewLoaded and other.
- Asynchronous overrides for Screen's methods: OnInitialize, OnActivate, ... You can override then as
protected override async void OnInitialize(){}
and inside you can await another task. - Convert Coroutines to Tasks. Use
ExecuteAsync()
extension method. Coroutines still have some advantages in some scenarios, like execution context. IHandleWithTask<TMessage>
- pretty handy...
有的博客文章desribing一些使用的病例很少code片段。和 GitHub的版本库我用用异步玩示例项目/等待在卡利。
There's a blog post desribing some use cases with few code snippets. And a GitHub repository with sample project I've used to play with async/await in Caliburn.
这篇关于将caliburn.micro做与视图模型异步方法正确的事情?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!