共用方式為


Event.split<'T,'U1,'U2,'Del> 函式 (F#)

更新:2010 年 9 月

傳回新的事件的原始事件接聽及觸發第一個產生事件,如果函式的應用程式事件的引數傳回 Choice1Of2,和第二個事件,如果它傳回一個 Choice2Of2

命名空間/模組路徑: Microsoft.FSharp.Control.Event

組件:FSharp.Core (在 FSharp.Core.dll 中)

// Signature:
Event.split : ('T -> Choice<'U1,'U2>) -> IEvent<'Del,'T> -> IEvent<'U1> * IEvent<'U2> (requires delegate)

// Usage:
Event.split splitter sourceEvent

參數

  • splitter
    Type: 'T -> Choice<'U1,'U2>

    函數中,辨識通常是使用中的模式器,事件值轉換兩種類型之一。

  • sourceEvent
    型別:IEvent<'Del,'T>

    輸入事件。

傳回值

事件的 Tuple。 每當 splitter 評估為 Choice1of1 時會觸發第一個事件,splitter 評估為 Choice2of2 時則會觸發第二個事件。

備註

這個函式是名為 Split中 已編譯的組件。 如果從一個語言,F # 以外,或透過反映存取函式使用這個名稱。

範例

下列程式碼將示範如何使用 Event.split函式來實作能夠移動控制項上的 表單。 splitter函式是使用中的模式辨識器 (|Down|Up|),用來表示狀態的 滑鼠按鈕。 如果使用者按下滑鼠按鈕,在按鈕上時,請移動滑鼠的同時,按鈕移開。 也是有時變更按鈕的色彩,雖然它在移動,取決於哪一個滑鼠按鈕所使用的程式碼。 每個滑鼠按鈕,這項測試會使用不同的色彩。 放開按鈕後,在其他事件路徑,這使用滑鼠按鈕不是向下時,會還原原來的色彩] 按鈕。

open System.Windows.Forms
open System.Drawing
open Microsoft.FSharp.Core

let form = new Form(Text = "F# Windows Form",
                    Visible = true,
                    TopMost = true)

let button = new Button(Text = "Button",
                        Visible = true,
                        Left = 100,
                        Width = 50,
                        Top = 100,
                        Height = 20)

form.Controls.Add(button)
let originalColor = button.BackColor
let mutable xOff, yOff = (0, 0)

let (|Down|Up|) (evArgs:MouseEventArgs) =
    match evArgs.Button with
    | MouseButtons.Left 
    | MouseButtons.Right 
    | MouseButtons.Middle -> Down(evArgs)
    | _ -> Up

button.MouseDown 
|> Event.add(fun evArgs ->
    xOff <- evArgs.X
    yOff <- evArgs.Y)

form.MouseMove
|> Event.map (fun evArgs -> (evArgs.X, evArgs.Y))
|> Event.add (fun (x, y) -> form.Text <- sprintf "(%d, %d)" x y)

let (dragButton, noDragButton) = Event.split (|Down|Up|) button.MouseMove

// Move the button, and change its color if the user uses the
// right or middle mouse button.
dragButton |> Event.add ( fun evArgs ->
    match evArgs.Button with
    | MouseButtons.Left ->
        button.BackColor <- originalColor
    | MouseButtons.Right ->
        button.BackColor <- Color.Red
    | MouseButtons.Middle ->
        button.BackColor <- Color.Blue
    | _ -> ()
    button.Left <- button.Left + evArgs.X - xOff
    button.Top <- button.Top + evArgs.Y - yOff
    button.Refresh())

// Restore the button's original color when the mouse is moved after
// the release of the button.
noDragButton |> Event.add ( fun () -> 
    button.BackColor <- originalColor)

平台

Windows 7、Windows Vista SP2、Windows XP SP3、Windows XP x64 SP2、Windows Server 2008 R2、Windows Server 2008 SP2、Windows Server 2003 SP2

版本資訊

F# 執行階段

支援版本:2.0、4.0

Silverlight

支援版本:3

請參閱

參考

Control.Event 模組 (F#)

Microsoft.FSharp.Control 命名空間 (F#)

變更記錄

日期

History

原因

2010 年 9 月

加入程式碼範例。

資訊加強。