FSI WPF事件循环 [英] FSI WPF Event Loop

查看:109
本文介绍了FSI WPF事件循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个答案对于FSI仍然是一个很好的答案(除了rethrow现在是reraise)?答案是从2008年开始的,我不确定周围是否有任何更好"的实现.如果不是,那会是什么?

Is the WPF event loop in this answer still a good one for FSI (besides rethrow which is now reraise)? The answer is from 2008 and I'm not sure if there are any "better" implementations around. If not what would one be?

我的了解是默认实现用于WinForms.

My understanding is that the default implementation is for WinForms.

推荐答案

是的,默认是Winforms,我确实使用了WpfEventLoop,代码在下面,

Yes the default is for Winforms, I do use the WpfEventLoop quite a lot, Code is below,

#I "c:/Program Files/Reference Assemblies/Microsoft/Framework/v3.0"
#I "C:/WINDOWS/Microsoft.NET/Framework/v3.0/WPF/"
#r "PresentationCore.dll"
#r "PresentationFramework.dll"
#r "WindowsBase.dll"

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()

测试代码

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

let window = new Window(Title = "Simple Test", Width = 800., Height = 600.)
window.Show()

let textBox = new TextBox(Text = "F# is fun")
window.Content <- textBox

让我知道这是否有帮助.

Let me know if this helps.

-法哈德

这篇关于FSI WPF事件循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆