How can I use WPF menus and dialogs in F #?

I am trying to find an example of using XAML and F # - without C # - to customize traditional menus and dialogs. Everything I can find on the Internet either uses C # or the old one, up to the latest versions of F # and .NET. Can anyone suggest an example that I can look at? Thanks.

+5
source share
1 answer

When you try to learn WPF, you come across many C # examples based on "good old" code, not MVVM or MVC. The following explains how to quickly create F # WPF code behind an application. Using this, it is easier to experiment with all of these examples.

Create a F # console application.

Change the Output Type of the application to the Windows application.

Add FsXaml from NuGet.

Add these four source files and arrange them in that order.

MainWindow.xaml

<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 exit code. 

Delete the program.fs file.

Change Build Action to Resource for two xaml files.

Add a reference to the .NET assembly UIAutomationTypes .

Compile and run.

You cannot use the constructor to add event handlers. Just add them manually to the code behind.

StackOverflow may not be the best place to post full demos like this, especially if it raises more questions on the same line. If there is another better place, for example. public repo for this kind of thing, please let me know.

+3
source

Source: https://habr.com/ru/post/1263528/


All Articles