内容目录
创建一个元素:
<Label
x:Name="Label1"
Text="111"
VerticalOptions="Center"
HorizontalOptions="Center" />
<Button Text="点击" Clicked="OnButtonClicked"></Button>
定义一个模型,继承 INotifyPropertyChanged。
public class A : INotifyPropertyChanged
{
private string _name;
public string Name
{
get => _name; set
{
_name = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
绑定:
InitializeComponent();
Label1.SetBinding(Label.TextProperty, "Name");
Label1.BindingContext = A;
使用:
public A A { get; set; } = new A
{
Name = "111"
};
public void OnButtonClicked(object? sender, EventArgs e)
{
A.Name = DateTime.UtcNow.ToString();
var c = Label1.Text;
}
文章评论