c# - 如何在 Xamarin Forms 中使用 Object Initializer 为 Label 添加 setbinding

标签 c# xamarin binding label xamarin.forms

有没有办法在 Xamarin Forms 中使用对象初始值设定项在标签内使用设置绑定(bind)

a) Label lb = new Label();
   lb.setBinding(----);


b) new Label{
**Set Binding** ???
}

最佳答案

,您不能使用object initializer设置控件的绑定(bind)。

因为(definition from MSDN): 对象初始值设定项允许您在创建时为对象的任何可访问字段或属性赋值,而无需调用构造函数后接几行赋值语句。

您不能使用 SetBinding在 Object initilaizer 因为它不是 property或可访问字段,而是 method .

您必须按如下方式使用 SetBinding :

var label = new Label ();
label.SetBinding (Label.TextProperty, "Name");
label.BindingContext = new {Name = "John Doe", Company = "Xamarin"};

关于c# - 如何在 Xamarin Forms 中使用 Object Initializer 为 Label 添加 setbinding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38696044/

相关文章:

xaml - 根据 bool 值更改 Xamarin 表单标签的文本

python - 无法使用 NetMQ 在 Unity3d 和 Python 之间发送消息

android - 单核细胞增多症。 Xamarin。加载 svg 图像时出错。 System.MethodAccessException异常

c# - 尝试使用带有 c# 的 xamarin android 打开默认浏览器

c# - 使用 MVVM 绑定(bind) ComboBox SelectedItem

wpf - 有没有办法判断 DependencyProperty 的值是来自绑定(bind)源还是目标?

c# - 记录 C# 代码的规则/指南?

c# - 保存对话框下载文件,将文件从 ASP.NET 服务器保存到客户端

c# - 像在 C# 中一样在 C++ 中绑定(bind)

xamarin.ios - 我如何知道我安装了哪个版本的单点触控?