在 Xamarin 表单中,我们在控件上设置绑定(bind),例如:
myLabel.SetBinding<MyViewModel>(Label.TextProperty, viewModel => viewModel.LabelText);
有没有办法将第二个参数(lambda 表达式)存储在变量中?
基于 this answer ,我试过:
Func<MyViewModel, string> myLambda = viewModel => viewModel.LabelText;
myLabel.SetBinding<MyViewModel>(Label.TextProperty, myLambda);
但是第二个参数有错误的红色下划线
cannot convert from 'System.Func<someViewModel, someType>' to 'System.Linq.Expressions<System.Func<someViewModel, object>>'
最佳答案
是的。通用 source
本例中的参数类型为 Expression<Func<MyViewModel, string>>
, 不是 Func<MyViewModel, string>
.这两种类型都以相同的方式初始化,但含义却截然不同。参见
Why would you use Expression> rather than Func?
了解更多详情。
Expression<Func<MyViewModel, string>> myLambda;
myLambda = viewModel => viewModel.LabelText;
myLabel.SetBinding<MyViewModel>(Label.TextProperty, myLambda);
关于c# - Xamarin.Forms - 可以对 SetBinding() lambda 参数进行变量化吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37008941/