简体   繁体   English

如何在Xamarin.Forms中进行数据绑定

[英]How to databind in the Xamarin.Forms

I am new to Xamarin.Forms. 我是Xamarin.Forms的新手。 I have an xaml file, that includes a label. 我有一个xaml文件,其中包含一个标签。 Also, I have instance of a custom object called tempObject . 另外,我有一个名为tempObject的自定义对象的实例。 It is of type TempObject . 它是TempObject类型的。

I need to display values of TempObject on my UI. 我需要在UI上显示TempObject值。

TempObject has Name and Description as public string element. TempObject具有名称和描述作为公共字符串元素。

I tried following code: 我尝试了以下代码:

<Label Text="{Binding ElementName=tempObject , Path=Name}" TextColor="#0099DC" />

It throws exception. 它引发异常。

Could you please help me how to bind an object to Labels in my xaml file. 您能否帮助我如何将对象绑定到xaml文件中的Labels。

What is the BindingContext in your page? 您页面中的BindingContext是什么? Using the ElementName is not correct in this scenario. 在这种情况下,使用ElementName是不正确的。

You can use the {Binding tempObject.Name} if tempObject object available in the context 如果上下文中有tempObject对象,则可以使用{Binding tempObject.Name}

You may want to read through the Databinding basics for Xamarin.Forms https://developer.xamarin.com/guides/xamarin-forms/xaml/xaml-basics/data_binding_basics/ 您可能想通读Xamarin.Forms的数据绑定基础知识https://developer.xamarin.com/guides/xamarin-forms/xaml/xaml-basics/data_binding_basics/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM