中使用WPF菜单和对话框

中使用WPF菜单和对话框

本文介绍了如何在F#中使用WPF菜单和对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找使用XAML和F#(不使用C#)来设置传统菜单和对话框的示例.我可以在网上找到的所有内容都使用C#,或者使用的都是旧版本,而不是最新版本的F#和.NET.任何人都可以提出一个我可以看的例子吗?谢谢.

I've been trying to find an example of using XAML and F# - without C# - to set up traditional menus and dialog boxes. Everything I can find online either uses C# or it is old, before the most recent versions of F# and .NET. Can anyone suggest an example I can look at? Thanks.

推荐答案

当您尝试学习WPF时,会遇到许多基于旧版本"代码而不是MVVM或MVC的C#示例.下面说明如何在应用程序后面快速创建F#WPF代码.使用此工具,可以更轻松地尝试所有这些示例.

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

创建一个F#控制台应用程序.

Create an F# console application.

将应用程序的输出类型更改为 Windows应用程序.

从NuGet中添加 FsXaml .

添加这四个源文件,并按此顺序排列.

Add these four source files, and arrange them in this order.

MainWindow.xaml

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

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

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文件的生成操作更改为资源.

Change the Build Action to Resource for the two xaml files.

添加对.NET程序集的引用 UIAutomationTypes .

Add a reference to the .NET assembly UIAutomationTypes.

编译并运行.

您不能使用设计器添加事件处理程序.只需在后面的代码中手动添加它们即可.

You can't use the designer to add event handlers. Simply add them manually in the code behind.

StackOverflow可能不是发布像这样的完整演示的最佳场所,特别是如果这会在同一行引发更多问题的话.如果还有另一个更好的地方,例如这类事情的公共仓库,请让我知道.

StackOverflow is possibly not the best place to post complete demos like this one, especially if this spins off more questions along the same line. If there is another better place, e.g. a public repo for this kind of thing, please let me know.

这篇关于如何在F#中使用WPF菜单和对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:05