本文介绍了等待的同时F#多个事​​件的任何情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在F#我知道如何以异步方式等待使用一个事件:

 让测试= {异步
  让!此举= Async.AwaitEvent(form.MouseMove)
  ......把手举...}

假设我想等到了或<$c$c>KeyDown事件。我想有这样的事情:

 让! moveOrKeyDown = Async.AwaitEvent(form.MouseMove,form.KeyDown)

此功能不存在,但有另一种方式来做到这一点?


解决方案

 让ignoreEvent E = Event.map忽略Ë让合并= Event.merge(ignoreEvent f.KeyDown)(ignoreEvent f.MouseMove)
Async.AwaitEvent合并

编辑:另一个preserves版本原稿类型

 让合并= Event.merge(f.KeyDown |&GT; Event.map Choice1Of2)(f.MouseMove |&GT; Event.map Choice2Of2)
Async.AwaitEvent合并

编辑2 :根据意见的托马斯Petricek

 让E1 = f.KeyDown |&GT; Observable.map Choice1Of2
让E2 = f.MouseMove |&GT; Observable.map Choice2Of2
让! EVT = Observable.merge E1 E2 |&GT; Async.AwaitObservable

AwaitObservable 的原始可以从这里采取A>('无演示在Silverlight'由Tomas Petricek)。

In F# I know how to wait asynchronously for one event using Async.AwaitEvent:

let test = async {
  let! move = Async.AwaitEvent(form.MouseMove)
  ...handle move... }

Suppose I want to wait for either the MouseMove or the KeyDown event. I'd like to have something like this:

let! moveOrKeyDown = Async.AwaitEvent(form.MouseMove, form.KeyDown)

This function doesn't exist but is there another way to do this?

解决方案
let ignoreEvent e = Event.map ignore e

let merged = Event.merge (ignoreEvent f.KeyDown) (ignoreEvent f.MouseMove)
Async.AwaitEvent merged

EDIT: another version that preserves original types

let merged = Event.merge (f.KeyDown |> Event.map Choice1Of2) (f.MouseMove |> Event.map Choice2Of2)
Async.AwaitEvent merged

EDIT 2: according to comments of Tomas Petricek

let e1 = f.KeyDown |> Observable.map Choice1Of2
let e2 = f.MouseMove |> Observable.map Choice2Of2
let! evt = Observable.merge e1 e2 |> Async.AwaitObservable

AwaitObservable primitive can be taken from here ('Reactive demos in Silverlight' by Tomas Petricek).

这篇关于等待的同时F#多个事​​件的任何情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 11:24