Let's say I have this xaml code :
假设我有这个xaml代码:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Canvas>
<Rectangle Name="papan" Fill="Red" Height="20" Width="20" />
</Canvas>
</Window>
And I have a file named Program.fs (F# code), how can I access "papan", for example, from my code?
我有一个名为Program.fs(F#代码)的文件,如何从我的代码中访问“papan”?
Thanks
谢谢
2 个解决方案
#1
1
Something like this?
像这样的东西?
open System.Windows
open System.Windows.Markup
open System.Windows.Shapes
let xaml = @"<Window xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<Canvas>
<Rectangle Name='papan' Fill='Red' Height='20' Width='20' />
</Canvas>
</Window>"
let wnd = XamlReader.Parse(xaml) :?> Window
let rect = wnd.FindName("papan") :?> Rectangle
If the XAML is in a separate file, you can use XamlReader.Load
instead of XamlReader.Parse
.
如果XAML位于单独的文件中,则可以使用XamlReader.Load而不是XamlReader.Parse。
#2
4
In addition to the answer posted by kvb, you can use F# dynamic invoke operator (which is similar to dynamic
in C# 4.0) to get a nicer syntax. The operator allows you to define the meaning of expressions such as wnd?papan
. You can for example specify that this should perform the lookup using FindName
method. The operator definition looks like this:
除了kvb发布的答案之外,您还可以使用F#动态调用运算符(类似于C#4.0中的动态)来获得更好的语法。运算符允许您定义表达式的含义,例如wnd?papan。例如,您可以指定这应该使用FindName方法执行查找。运算符定义如下所示:
let (?) (this : Control) (prop : string) : 'T = // '
this.FindName(prop) :?> 'T
Then you can just write:
然后你可以写:
let rect : Rectangle = wnd?papan
You still need to write the type (Rectangle
) explicitly, so that F# type inference can use it, but the syntax is a bit more comfortable.
您仍然需要显式地编写类型(Rectangle),以便F#类型推断可以使用它,但语法更舒适一点。
#1
1
Something like this?
像这样的东西?
open System.Windows
open System.Windows.Markup
open System.Windows.Shapes
let xaml = @"<Window xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<Canvas>
<Rectangle Name='papan' Fill='Red' Height='20' Width='20' />
</Canvas>
</Window>"
let wnd = XamlReader.Parse(xaml) :?> Window
let rect = wnd.FindName("papan") :?> Rectangle
If the XAML is in a separate file, you can use XamlReader.Load
instead of XamlReader.Parse
.
如果XAML位于单独的文件中,则可以使用XamlReader.Load而不是XamlReader.Parse。
#2
4
In addition to the answer posted by kvb, you can use F# dynamic invoke operator (which is similar to dynamic
in C# 4.0) to get a nicer syntax. The operator allows you to define the meaning of expressions such as wnd?papan
. You can for example specify that this should perform the lookup using FindName
method. The operator definition looks like this:
除了kvb发布的答案之外,您还可以使用F#动态调用运算符(类似于C#4.0中的动态)来获得更好的语法。运算符允许您定义表达式的含义,例如wnd?papan。例如,您可以指定这应该使用FindName方法执行查找。运算符定义如下所示:
let (?) (this : Control) (prop : string) : 'T = // '
this.FindName(prop) :?> 'T
Then you can just write:
然后你可以写:
let rect : Rectangle = wnd?papan
You still need to write the type (Rectangle
) explicitly, so that F# type inference can use it, but the syntax is a bit more comfortable.
您仍然需要显式地编写类型(Rectangle),以便F#类型推断可以使用它,但语法更舒适一点。