fsi event loop
2015-08-10 15:29
369 查看
在默认情况下fsi 中的Event loop是绑定到winform中的,如果我们在fsi中测试WPF的控件时,加载自定义的WPF控件,有些消息就无法正常响应。比如按键消息就无法正常响应。这时需要重定义fsi的Event loop。好在有现成的示例。
module WPFEventLoop =
open System
open System.Windows
open System.Windows.Threading
open Microsoft.FSharp.Compiler.Interactive
open Microsoft.FSharp.Compiler.Interactive.Settings
type RunDelegate<'b> = delegate of unit -> 'b
let Create() =
let app =
try
// Ensure the current application exists. This may fail, if it already does.
let app = new Application() in
// Create a dummy window to act as the main window for the application.
// Because we're in FSI we never want to clean this up.
new Window() |> ignore;
app
with :? InvalidOperationException -> Application.Current
let disp = app.Dispatcher
let restart = ref false
{ new IEventLoop with
member x.Run() =
app.Run() |> ignore
!restart
member x.Invoke(f) =
try
disp.Invoke(DispatcherPriority.Send,new RunDelegate<_>(fun () -> box(f ()))) |> unbox
with e -> eprintf "\n\n ERROR: %O\n" e; reraise()
member x.ScheduleRestart() = ()
//restart := true;
//app.Shutdown()
}
let Install() = fsi.EventLoop <- Create()
WPFEventLoop.Install()
运行之后按键消息就可以正常响应了。
module WPFEventLoop =
open System
open System.Windows
open System.Windows.Threading
open Microsoft.FSharp.Compiler.Interactive
open Microsoft.FSharp.Compiler.Interactive.Settings
type RunDelegate<'b> = delegate of unit -> 'b
let Create() =
let app =
try
// Ensure the current application exists. This may fail, if it already does.
let app = new Application() in
// Create a dummy window to act as the main window for the application.
// Because we're in FSI we never want to clean this up.
new Window() |> ignore;
app
with :? InvalidOperationException -> Application.Current
let disp = app.Dispatcher
let restart = ref false
{ new IEventLoop with
member x.Run() =
app.Run() |> ignore
!restart
member x.Invoke(f) =
try
disp.Invoke(DispatcherPriority.Send,new RunDelegate<_>(fun () -> box(f ()))) |> unbox
with e -> eprintf "\n\n ERROR: %O\n" e; reraise()
member x.ScheduleRestart() = ()
//restart := true;
//app.Shutdown()
}
let Install() = fsi.EventLoop <- Create()
WPFEventLoop.Install()
运行之后按键消息就可以正常响应了。
相关文章推荐
- JAVA实现监测tomcat是否宕机及控制重启的方法
- 我想在linux下用json
- 【转】Linux Page Cache的工作原理
- LINUX-KVM 基本原理及架构
- OpenGL+MFC对glMatrixMode(),glLoadIdentity()的理解
- windows配置nginx实现负载均衡集群
- 子元素设置margin-top,父元素也受影响
- 现代OpenGL教程 02 - 贴图
- linux系统怎么增加swap分区空间小?
- 现代OpenGL教程 01 - 入门指南
- ecshop二次开发:Non-static method cls_image::gd_version() should not be called statically in的解决
- zabbbix 系统时间监控
- Linux - RAID和LVM
- Linux - RAID和LVM
- BASH练习第四课
- POJ 2976-Dropping tests(01分数规划_二分||Dinkelbach算法)
- BASH练习第三课
- apache tomcat集群
- apache tomcat集群
- LINUX下SVN系统的搭建