Skip to content

Instantly share code, notes, and snippets.

@schuster-rainer
Created July 6, 2012 19:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schuster-rainer/3062194 to your computer and use it in GitHub Desktop.
Save schuster-rainer/3062194 to your computer and use it in GitHub Desktop.
Run a WPF Window from ClojureCLR and modify it at the REPL
;reference wpf assemblys
(import '(System.Reflection Assembly))
(Assembly/LoadWithPartialName "PresentationFramework")
(Assembly/LoadWithPartialName "PresentationCore")
(Assembly/LoadWithPartialName "WindowsBase")
(ns wpf
(:import (System.Windows Application Window))
(:import (System.Windows.Threading Dispatcher DispatcherPriority))
(:import (System.Threading ApartmentState ThreadStart Thread AutoResetEvent))
(:gen-class))
(def win (atom nil))
(defn -main [& args]
(let [app (new Application)]
(reset! win (new Window))
(.set_Title @win "Hello World")
(.Run app @win)))
(defn sta-thread [func]
(let [delegate (gen-delegate ThreadStart [] (func))
thread (doto (new Thread delegate)
(.SetApartmentState ApartmentState/STA)
(.Start))]
thread))
(defn wpf-eval
[uithread repl-ns-sym data]
(.Invoke (Dispatcher/FromThread uithread) DispatcherPriority/Normal
(gen-delegate Action []
(clojure.main/with-bindings
(in-ns repl-ns-sym)
(eval data)))))
(let [uithread (sta-thread -main)]
(clojure.main/repl :eval (partial wpf-eval uithread 'wpf)))
; REPL usage
; wpf=> (.set_Title @win "Hello from REPL!")
; nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment