首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用按钮从WPF调用F#函数(FsXaml方法)

使用按钮从WPF调用F#函数(FsXaml方法)
EN

Stack Overflow用户
提问于 2019-01-09 21:55:34
回答 1查看 240关注 0票数 1

我正在尝试用F#,WPF和FsXaml创建一个按钮Hello World应用程序。我开始遵循这个指南:

https://www.c-sharpcorner.com/article/create-wpf-application-with-f-sharp-and-fsxaml/

当我只是在xaml上加载程序并进行编译时,一切都很正常,但是我还没有设法通过按下按钮来调用函数,在他解释如何调用函数之前,指南就结束了。

我已经看到了许多不同的方法,但还没有一种方法对我有效(而且许多指南都已经有几年的历史了,所以从那以后框架内部发生了很多事情)。一旦我理解了在使用FsXaml时x.xaml和x.xaml.fs之间的逻辑,就能有一个有效的(且简单的)起点开始构建,那就太好了。

我在MainWindow.xaml上的按钮:

代码语言:javascript
复制
<Button x:Name="submitButton" Content="Send" Click="submitButton_Click"/>

我在MainWindow.xaml的window -section中也有这个:

代码语言:javascript
复制
xmlns:local="clr-namespace:Views;assembly=GUItemplate"

我的MainWindow.xaml.fs:

代码语言:javascript
复制
namespace GUItemplate

open FsXaml  
open System.Windows

type MainWindowBase = XAML<"MainWindow.xaml">

type MainWindow() =
    inherit MainWindowBase()

    override this.submitButton_Click (sender: obj, e: RoutedEventArgs) = 
        MessageBox.Show("Hello world!")
        |> ignore

我目前得到的错误:

代码语言:javascript
复制
System.Windows.Markup.XamlParseException
HResult=0x80131501
Message='Failed to create a 'Click' from the text 'submitButton_Click'.' Line number '29' and line position '101'.
Source=PresentationFramework

Inner Exception 1:
ArgumentException: Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.
EN

Stack Overflow用户

发布于 2019-01-10 23:54:01

这就是我在VS 2017中的工作方式,对我来说,它是有效的。我添加了UIAutomationTypes引用并安装了NuGet FsXaml.Wpf

代码语言:javascript
复制
open System
open System.Windows
open FsXaml

type MainWindowBase = XAML<"MainWindow.xaml">

type MainWindow() =
    inherit MainWindowBase()

    override this.submitButton_Click (sender: obj, e: RoutedEventArgs) = 
        MessageBox.Show("Hello world!")
        |> ignore

[<EntryPoint;STAThread>]  
let application = new Application() in
    let mainWindow = new MainWindow() in
        application.Run(mainWindow) |> ignore    
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54111774

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档