Created
July 10, 2012 14:36
-
-
Save schuster-rainer/3083679 to your computer and use it in GitHub Desktop.
Run a WPF Window loaded from a xaml file in ClojureCLR and modify it at the REPL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;reference wpf assemblys | |
(import '(System.Reflection Assembly)) | |
(Assembly/LoadWithPartialName "PresentationFramework") | |
(Assembly/LoadWithPartialName "PresentationCore") | |
(Assembly/LoadWithPartialName "WindowsBase") | |
(Assembly/LoadWithPartialName "System.Xml") | |
(ns wpf | |
(:import (System.Windows Application Window)) | |
(:import (System.Windows.Threading Dispatcher DispatcherPriority)) | |
(:import (System.Threading ApartmentState ThreadStart Thread AutoResetEvent)) | |
(:import (System.IO File)) | |
(:import (System.Xml XmlReader)) | |
(:import (System.Windows.Markup XamlReader)) | |
; (:require [clojure.tools.cli :as c]) | |
(:gen-class)) | |
(def win (atom nil)) | |
(defn -main [& args] | |
; (let [[options args banner] | |
; (c/cli args | |
; ["-x" "--xaml" "xaml file to load with window as root" :default "MainWindow.xaml"])] | |
; (comment | |
(let [app (new Application)] | |
(reset! win (-> (first args) | |
File/OpenText | |
XmlReader/Create | |
XamlReader/Load)) | |
(.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 (apply partial -main *command-line-args*))] | |
(clojure.main/repl :eval (partial wpf-eval uithread 'wpf))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment