如何解决如何在 Xamarin 中将绑定值从一个类传递到另一个类? 在信息类在页面中
我有一个看起来像这样的课程。我想在其中创建一个 Info
类,并将 Info
中的文本值绑定到视图模型 (_vm.A)。我知道如何将字符串传递到 Info 类中,但如何传递随时间变化的绑定值。
public class InfoPage
{
private readonly Infoviewmodel _vm;
public InfoPage() : base()
{
BindingContext = _vm = new Infoviewmodel();
var info = new Info(_vm.A);
}
public ChangeValueX() {
_vm.A = "XXX";
}
public ChangeValueY() {
_vm.A = "YYY";
}
}
public Info(??? ???)
{
var Label1 = new Label()
.Bind(Label.TextProperty,???,source: this);
}
有人能告诉我如何将绑定值传递给类 Info
以便当 viewmodel 中的绑定值更改时文本也会更改。由于我不知道该怎么做,所以我现在只使用问号。
注意 viewmodel 如下所示:
private string _a;
public string A{ get => _a; set => SetProperty(ref _a,value); }
解决方法
在您的情况下,您可以使用 BindableProperty
在信息类
public class Info:BindableObject
{
public static readonly BindableProperty StringValueProperty =
BindableProperty.Create("StringValue",typeof(string),typeof(Info),string.Empty);
public string StringValue
{
get { return (string)GetValue(StringValueProperty); }
set { SetValue(StringValueProperty,value); }
}
public Info()
{
Label label = new Label();
label.SetBinding(Label.TextProperty,new Binding("StringValue",source: this));
}
}
在页面中
Info info = new Info();
info.SetBinding(Info.StringValueProperty,new Binding("xxx",source: this));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。