简体   繁体   中英

F# Event is being triggered multiple times . . .?

I have this really simple program to get RSS-feeds from a website and populate a listbox with the items. Whenever the user selects an item and presses Enter, it should go to a web-page. this is the KeyUp event handler!

rssList.KeyUp
|> Event.filter (fun e -> rssList.SelectedItems.Count > 0)
|> Event.filter (fun (args:Input.KeyEventArgs) -> args.Key = Key.Enter)
|> Event.add  -> let feed = unbox<RSSFeed>  rssList.SelectItem)
                                            Process.Start(feed.Link) |> ignore)

What I'm getting is the following:

  • the first time the event triggers, it works fine, the browser open and a page is loaded
  • the second time it triggers TWO times, so now i get two browser windows opened and the page is loaded in both of them.
  • the third time I get Three browser . . . You get the idea!

Anybody any idea why this is happening? My goal is (you guessed it) just to open 1 browser window and 1 page PER trigger

There are several compilation errors in your example including a malformed lambda expression, mismatched parentheses, incorrect identitfiers ( SelectItem is not a property, I'm assuming you mean SelectedItem not SelectedItems ), and incorrect indentation following the let feed binding.

Below is a simplified example that works as you intended. The selected item in the top ListBox is put into the bottom ListBox when the user hits Enter.

open System
open System.Windows
open System.Windows.Controls
open System.Windows.Input

[<EntryPoint>]
[<STAThread>]
let main argv = 
    let panel = new DockPanel()
    let listBox = new ListBox()
    for i in [| 1 .. 10 |] do
        listBox.Items.Add i |> ignore
    DockPanel.SetDock(listBox, Dock.Top)
    let listBox2 = new ListBox(Height = Double.NaN)
    panel.Children.Add listBox |> ignore
    panel.Children.Add listBox2 |> ignore
    listBox.KeyUp
        |> Event.filter (fun e -> listBox.SelectedItems.Count > 0)
        |> Event.filter (fun e -> e.Key = Key.Enter)
        |> Event.add (fun e ->  let i = unbox<int> listBox.SelectedItem
                                listBox2.Items.Add(i) |> ignore)
    let win = new Window(Content = panel)
    let application = new Application()
    application.Run(win) |> ignore
    0

I got it working properly by implementing the Handled property of the event args with the following:

let doubleClick = new MouseButtonEventHandler(fun sender (args:MouseButtonEventArgs) ->
    let listBox = unbox<ListBox> sender
    match listBox.SelectedItems.Count > 0 with
    | true -> 
        let listBox = unbox<ListBox> sender
        let feed = unbox<RSSFeed> listBox.SelectedItem
        Process.Start(feed.Link) |> ignore
        args.Handled <- true; ()
    | false -> 
        args.Handled <- true; ())

thanks to everyone who helped me here!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM