通过引用默认的 WPF DLL,使用纯代码 WPF 可以很容易地做任何你可以做的事情:
#r "PresentationCore.dll"
#r "PresentationFramework.dll"
// ...other DLLs...
#r "WindowsBase.dll"
let window = System.Windows.Window()
let panel = System.Windows.Controls.StackPanel()
let button = System.Windows.Controls.Button()
panel.Children.Add button
button.Content <- "hi"
window.Content <- panel
window.Show()
...并且您可以在窗口仍然打开时对其进行操作...
button.Click.Add (fun _ ->
button.Content <-
button.Content :?> string |> fun x -> (x + "!") :> obj)
...然后单击按钮查看它的工作情况。这似乎是构建 UI 组件的一种非常强大的方法。
有没有办法对 Windows.UI 命名空间/控件/UI 框架做同样的事情——在 F# 交互中加载一些程序集并动态实例化 UI 组件?
我天真地尝试引用似乎相关的文件:
#r @"C:\Program Files (x86)\Windows Kits\10\References\Windows.Foundation.UniversalApiContract\2.0.0.0\Windows.Foundation.UniversalApiContract.winmd"
#r @"C:\Program Files (x86)\Windows Kits\10\References\Windows.Foundation.FoundationContract\2.0.0.0\Windows.Foundation.FoundationContract.winmd"
...这样做让我智能感知到 Windows.UI 命名空间。但是当我尝试实例化某些东西时:
Windows.UI.Xaml.Application()
我得到:
error FS0193: Could not load file or assembly 'file:///C:\Program Files (x86)\Windows Kits\10\References\Windows.Foundation.UniversalApiContract\2.0.0.0\Windows.Foundation.UniversalApiContract.winmd' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
最佳答案
我的理解是目前还没有对 UWP 的 F# 支持。
例如,参见这个新鲜 open issue .
关于f# - F# 交互中的 Windows UI(UWP 或 8.1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38343566/