Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Silverlight 在xaml中添加f#函数作为事件处理程序_Silverlight_Events_Xaml_F# - Fatal编程技术网

Silverlight 在xaml中添加f#函数作为事件处理程序

Silverlight 在xaml中添加f#函数作为事件处理程序,silverlight,events,xaml,f#,Silverlight,Events,Xaml,F#,有人知道如何将F#事件处理程序添加到xaml文件中的控件吗?基本上,您需要加载xaml文件并按名称查找控件: let w = Application.LoadComponent(new System.Uri( "/twitterstream;component/Window.xaml", System.UriKind.Relative )) :?> Window let b = w.FindName("buttonToggle") :?> But

有人知道如何将F#事件处理程序添加到xaml文件中的控件吗?

基本上,您需要加载xaml文件并按名称查找控件:

let w = Application.LoadComponent(new  System.Uri(
          "/twitterstream;component/Window.xaml", System.UriKind.Relative
        )) :?> Window
let b = w.FindName("buttonToggle") :?> Button
然后,您可以简单地将处理程序添加到事件:

b.Click.Add(fun _ -> ...)
您可以想象并使用一流的事件组合器-这里是一个很好的分步介绍:


我刚刚介绍了F#(伦敦)中使用Silverlight实现大多数示例的反应式编程。讲座已录制,样本也可下载,因此这可能是一个有用的资源:

  • 看我的博客
为了回答您的具体问题,我不认为您可以使用在XAML文件中指定事件处理程序的常规样式(这可能在F#Silverlight应用程序中有效,但您必须使用
member
而不是
let
函数)

然而,编写Silverlight组件的最佳方法(我能想到的)是只使用一个F#Silverlight库,并从C#Silverlight应用程序中使用它。在这种情况下,您需要在代码中编写事件处理程序绑定。一个简化的示例(来自演讲中的一个示例)如下所示:

open System.Windows.Controls

// Dynamic invoke operator that makes accessing XAML elements easy 
// (easier than using 'FindName' explicitly in the code
let (?) (this : Control) (prop : string) : 'T = // '
  this.FindName(prop) :?> 'T // '

type MyControl() as this =
  inherit UserControl()
  do 
    let path = "/MyProject;component/MyControl.xaml"
    let uri = new System.Uri(path, UriKind.Relative)
    Application.LoadComponent(this, uri)

    // Get Button named 'TestButton' from the XAML file
    let btn : Button = this?TestButton
    // Add event handler to the button
    btn.Add(fun _ -> btn.Text <- "Clicked!")
open System.Windows.Controls
//使访问XAML元素变得容易的动态调用运算符
//(比在代码中显式使用“FindName”更容易
让(?(this:Control)(prop:string):'T=/'
this.FindName(prop):?>'T/'
键入MyControl(),如下所示=
继承UserControl()
做
let path=“/MyProject;component/MyControl.xaml”
让uri=newsystem.uri(路径,UriKind.Relative)
LoadComponent(这个,uri)
//从XAML文件中获取名为“TestButton”的按钮
让btn:Button=this?TestButton
//将事件处理程序添加到按钮
btn.Add(乐趣->btn.Text