我一直在尝试寻找一个使用XAML和F#(不使用C#)来设置传统菜单和对话框的示例。我在网上找到的所有内容要么使用C#,要么是旧版本的F#和.NET。有没有人能推荐一个我可以参考的示例?谢谢。
我一直在尝试寻找一个使用XAML和F#(不使用C#)来设置传统菜单和对话框的示例。我在网上找到的所有内容要么使用C#,要么是旧版本的F#和.NET。有没有人能推荐一个我可以参考的示例?谢谢。
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="First Demo" Height="200" Width="300">
<Canvas>
<Button Name="btnTest" Content="Test" Canvas.Left="10" Canvas.Top="10" Height="28" Width="72"/>
</Canvas>
</Window>
MainWindow.xaml.fs
namespace FirstDemo
type MainWindowXaml = FsXaml.XAML<"MainWindow.xaml">
type MainWindow() =
inherit MainWindowXaml()
let whenLoaded _ =
()
let whenClosing _ =
()
let whenClosed _ =
()
let btnTestClick _ =
this.Title <- "Yup, it works!"
()
do
this.Loaded.Add whenLoaded
this.Closing.Add whenClosing
this.Closed.Add whenClosed
this.btnTest.Click.Add btnTestClick
App.xaml
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
</Application.Resources>
</Application>
App.xaml.fs
namespace FirstDemo
open System
open System.Windows
type App = FsXaml.XAML<"App.xaml">
module Main =
[<STAThread; EntryPoint>]
let main _ =
let app = App()
let mainWindow = new MainWindow()
app.Run(mainWindow) // Returns application's exit code.
删除Program.fs文件。
将两个xaml文件的Build Action更改为Resource。
添加对.NET程序集UIAutomationTypes的引用。
编译并运行。
您不能使用设计器添加事件处理程序。只需在代码后台手动添加它们。
在StackOverflow上发布完整演示可能不是最好的选择,特别是如果这会引发更多相同类型的问题。如果有其他更好的地方,例如此类事物的公共存储库,请告诉我。