我可以用F#以Xamarin形式创建StackLayout吗?
在他的博客帖子中,显示了正在创建的StackLayout:我可以用F#以Xamarin形式创建StackLayout吗?,f#,xamarin,xamarin.forms,F#,Xamarin,Xamarin.forms,在他的博客帖子中,显示了正在创建的StackLayout: StackLayout.Create( [ Entry (Placeholder = "Username") Entry (Placeholder = "Password", IsPassword = true) Button (Text = "Login", TextColor = Color.White, Backgro
StackLayout.Create(
[
Entry (Placeholder = "Username")
Entry (Placeholder = "Password", IsPassword = true)
Button (Text = "Login", TextColor = Color.White, BackgroundColor = Color.FromHex "77D065")
],...
但是,我得到一个错误,字段、构造函数或成员“Create”没有定义
如果我在参数之前取出.Create
,则错误消息将更改为对象构造函数“StackLayout”的成员,该成员接受0个参数,但此处给出1个参数
我的解释是Create是一个静态方法,使用元组,但我看不到它在部件浏览器中的任何位置定义。
在他的示例中,我还对TabbedPage上使用的一个小写的create
感到有点困扰,因此代码看起来不一致,可能是在未编译的情况下输入的,尽管他在下面显示了屏幕截图
我会采纳任何关于如何实现这一点的建议——如果没有缺少添加Create的神奇扩展,我很乐意采用其他方法
父类声明是:
[Xamarin.Forms.ContentProperty(“子项”)]
公共抽象类布局:布局,IViewContainer
其中T:Xamarin.Forms.View
我想有一种习惯用法可以将contentChildren
属性设置为C,但可能不设置为F
例如,我从中获得了编译代码,例如:
StackLayout StackLayout=新的StackLayout
{
间距=0,
VerticalOptions=LayoutOptions.FillAndExpand,
儿童=
{
新标签
{
Text=“StackLayout”,
水平选项=布局选项。开始
},
我尝试将其转换为F#语法-以下是合法语法,但设置了Children属性后,对象构造函数“StackLayout”的成员没有参数或可设置的返回属性“Children”。所需签名为StackLayout():unit。(FS0495)
所以,让我困惑不解——我不知道为什么孩子们可以从C ^而不是F中访问。唯一有意义的怀疑是,XAML内容属性注释在某种程度上影响了一个编译器而不是另一个编译器。
我是一个非常有经验的C++和Python程序员学习f*(和SWIFT),所以我很可能会在语法上绊倒。我得到元组,我习惯了逗号的奇怪角色,谢天谢地,我的Python背景使我对空白感到放松。
其他人在博客上的评论中也有同样的问题,但没有给出答案 我使用的是Xamarin.iOS 8.10.0.258 Xamarin.表格1.4.0.6341 我的退路是放弃使用F#作为GUI,而使用C#there和F#作为主逻辑,但我确实喜欢更紧凑的语法请注意,我也在他们的上询问了这一点。您显示的C#代码正在使用C#的集合初始化器功能,它将编译为调用
Children.Add()
,它没有设置Children
属性
F#没有相同的集合初始化器功能,因此您必须手动调用Children.Add()
,这很可能是StackLayout.Create
函数正在执行的操作
StackLayout.Create
函数可以是一个在名为StackLayout
的模块中定义的本地函数,这将解释为什么您在文档中看不到它。您显示的C代码正在使用C的集合初始化器功能,它将编译为对子项的调用。Add()
,它没有设置子属性
F#没有相同的集合初始化器功能,因此您必须手动调用Children.Add()
,这很可能是StackLayout.Create
函数正在执行的操作
StackLayout.Create
函数可以是一个在名为StackLayout
的模块中定义的本地函数,这将解释为什么您在文档中看不到它。您显示的C代码正在使用C的集合初始化器功能,它将编译为对子项的调用。Add()
,它没有设置子属性
F#没有相同的集合初始化器功能,因此您必须手动调用Children.Add()
,这很可能是StackLayout.Create
函数正在执行的操作
StackLayout.Create
函数可以是一个在名为StackLayout
的模块中定义的本地函数,这将解释为什么您在文档中看不到它。您显示的C代码正在使用C的集合初始化器功能,它将编译为对子项的调用。Add()
,它没有设置子属性
F#没有相同的集合初始化器功能,因此您必须手动调用Children.Add()
,这很可能是StackLayout.Create
函数正在执行的操作
StackLayout.Create
函数可以是一个在名为StackLayout
的模块中定义的本地函数,这可以解释为什么您在文档中看不到它。多亏了上面的Leaf Garland,这里有一个固定函数-我只需要提示C正在使用Add
type App() =
static member GetMainPage =
let sl = new StackLayout (
Spacing = 20.0,
VerticalOptions = LayoutOptions.FillAndExpand
)
let slc : View[] = [|
new Entry (Placeholder = "Username")
new Entry (Placeholder = "Password", IsPassword = true)
new Button (Text = "Login", TextColor = Color.White, BackgroundColor = Color.FromHex "77D065")
|]
slc |> Seq.iter (fun vc -> sl.Children.Add(vc))
new ContentPage( Content = sl )
注意,我必须创建temp变量slc
,以获得一个类型化为超类视图的数组
如果您拥有所有相同的类型,则可以使用文字列表和稍微简单的代码,如下所示:
type App() =
static member GetMainPage =
let sl = new StackLayout (
Spacing = 20.0,
VerticalOptions = LayoutOptions.FillAndExpand
)
[
new Label (Text = "StackLayout")
new Label (Text = "SuckLayout")
] |> Seq.iter (fun vc -> sl.Children.Add(vc))
new ContentPage( Content = sl )
关于C#collection初始化器,如果您感兴趣的话
今晚我太忙了,但将尝试编写StackLayout。创建函数作为练习。多亏了上面的Leaf Garland,这里有一个固定函数-我只需要C使用Add
的提示
type App() =
static member GetMainPage =
let sl = new StackLayout (
Spacing = 20.0,
VerticalOptions = LayoutOptions.FillAndExpand
)
let slc : View[] = [|
new Entry (Placeholder = "Username")
new Entry (Placeholder = "Password", IsPassword = true)
new Button (Text = "Login", TextColor = Color.White, BackgroundColor = Color.FromHex "77D065")
|]
slc |> Seq.iter (fun vc -> sl.Children.Add(vc))
new ContentPage( Content = sl )
注意,我必须创建temp变量slc
,以获得一个类型化为超类视图的数组
如果您拥有所有相同的类型,则可以使用文字列表和稍微简单的代码,如下所示:
type App() =
static member GetMainPage =
let sl = new StackLayout (
Spacing = 20.0,
VerticalOptions = LayoutOptions.FillAndExpand
)
[
new Label (Text = "StackLayout")
new Label (Text = "SuckLayout")
] |> Seq.iter (fun vc -> sl.Children.Add(vc))
new ContentPage( Content = sl )
关于C#collection initi