wpf - 在 .fs 文件中引用 .xaml 文件中的事件

标签 wpf xaml f# fsxaml

我想在 XAML 文件中声明一个事件,然后在 fs 文件中添加一个处理程序。

XAML 文件的上部类似于 (MouseRightButtonDown):

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AboutStack" MouseRightButtonDown="AboutStack_MouseRightButtonDown"
Title="About" SizeToContent="WidthAndHeight">
    <StackPanel>...

F# 文件包含:

open FsXaml

type MainWindow = XAML<"MainWindow.xaml", true> 
let mainwnd = new MainWindow()
let wnd = mainwnd.Root

有没有简单的方法来建立链接?

最佳答案

由于没有代码隐藏,您无法从 XAML 调用该方法,但需要从外部附加它:

let AboutStack_MouseRightButtonDown args =
    // do whatever you want, return unit

wnd.MouseRightButtonDown.Add(AboutStack_MouseRightButtonDown)

或者例如

wnd.MouseRightButtonDown.Add(fun _ -> MessageBox.Show("Click!") |> ignore)

另一种方法是使用模型(DataContext)和绑定(bind)来引发命令,请参见 Visual Studio Template for F# Windows App (WPF, MVVM)

关于wpf - 在 .fs 文件中引用 .xaml 文件中的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30806808/

相关文章:

wpf - Prism 还在积极开发中吗?

c# - 从 ICollectionView CurrentItem 的属性创建 CollectionViewSource

xaml - 为什么覆盖 ApplicationPageBackgroundThemeBrush 不起作用?

f# - 使用 FParsec 我将如何解析 : line ending in newline <|> a line ending with eof

c# - F# 中是否有类似于 Switch Case 命令的命令?

f# - 是否可以强制 .NET F# 编译器在模块中生成 CIL 字段?

c# - 如何访问 DataGrid.RowDetailsTemplate 中的控件?

c# - 使用Mvvm的通用Windows应用程序,无法使用资源resw文件

c# - 如果您想知道 Metro 应用程序何时关闭,应该注意什么事件?

wpf - ColumnDefinition MinWidth 不能正常工作