我使用下面的代码在C#中添加事件处理程序。它始终可在VS2017或VS2019中使用。
async void deviceChangedHandler(object s, EventArgs e) => await DevicesChangedEventAsync();
_deviceWatcher.DeviceChanged += deviceChangedHandler;
今天当我在VS2015中使用它时。我收到错误消息:当前上下文中不存在名称“异步”。不知道出了什么问题。有人可以帮忙吗?
直到VS 2017支持C#7.0才引入本地功能。
当然,您想要做的事情可以简化为:
_deviceWatcher.DeviceChanged += (_, args) => DevicesChangedEventAsync();
除了更短之外,还可以追溯到C#4.0(VS 2010)。实际上,这里不需要更新的语言功能,甚至没有帮助。