我有一个非常简单的程序来从网站获取RSS-feeds并使用这些项填充列表框。每当用户选择一个项目并按Enter键时,它应该转到一个网页。这是KeyUp事件处理程序!
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)
我得到的是以下内容:
有人知道为什么会这样吗?我的目标是(你猜对了)只打开1个浏览器窗口和1页PER触发器
答案 0 :(得分:1)
您的示例中有几个编译错误,包括格式错误的lambda表达式,不匹配的括号,不正确的identitfiers(SelectItem
不是属性,我假设您的意思是SelectedItem
而不是SelectedItems
),let feed
绑定后的缩进不正确。
以下是一个按预期工作的简化示例。当用户点击Enter时,顶部ListBox中的所选项目将放入底部ListBox。
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
答案 1 :(得分:-1)
我通过使用以下内容实现事件args的Handled属性使其正常工作:
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; ())
感谢所有在这里帮助我的人!