Data binding Xamarin.Forms刷新编辑器的TextProperty
有没有办法在事件发生后更改编辑器单元格中的文本 我有一个编辑器单元格,显示SQLite数据库中的地址。我还有一个按钮,可以获取当前地址,并在一个警报中显示,询问他们是否愿意将地址更新到此地址。如果是,那么我想在编辑器单元格中显示新地址Data binding Xamarin.Forms刷新编辑器的TextProperty,data-binding,inotifypropertychanged,xamarin.forms,Data Binding,Inotifypropertychanged,Xamarin.forms,有没有办法在事件发生后更改编辑器单元格中的文本 我有一个编辑器单元格,显示SQLite数据库中的地址。我还有一个按钮,可以获取当前地址,并在一个警报中显示,询问他们是否愿意将地址更新到此地址。如果是,那么我想在编辑器单元格中显示新地址 public class UserInfo : INotifyPropertyChanged { public string address; public string Address { get { return a
public class UserInfo : INotifyPropertyChanged
{
public string address;
public string Address
{
get { return address; }
set
{
if (value.Equals(address, StringComparison.Ordinal))
{
return;
}
address = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
我的编辑器单元格代码是
Editor userAddress = new Editor
{
BindingContext = uInfo, // have also tried uInfo.Address here
Text = uInfo.Address,
Keyboard = Keyboard.Text,
})
然后这个在它得到当前地址后我有这个
bool response = await DisplayAlert("Current Address", "Would you like to use this as your address?\n" + currAddress, "No", "Yes");
if (response)
{
//we will update the editor to show the current address
uInfo.Address = currAddress;
}
如何让它更新编辑器单元格以显示新地址?您正在设置控件的BindingContext,但没有指定与之配套的绑定。您希望将编辑器的TextProperty绑定到上下文的Address属性
Editor userAddress = new Editor
{
BindingContext = uinfo,
Keyboard = Keyboard.Text
};
// bind the TextProperty of the Editor to the Address property of your context
userAddress.SetBinding (Editor.TextProperty, "Address");
这也可能有效,但我不确定语法是否正确:
Editor userAddress = new Editor
{
BindingContext = uinfo,
Text = new Binding("Address"),
Keyboard = Keyboard.Text
};
谢谢你的快速回复。当我使用userAddress.SetBinding(Editor.TextProperty,“Address”);我得到一个System.InvalidCastException:无法从源类型转换到目标类型我非常确定语法是正确的。如果查看Xamarin ToDo示例,应该有一个编辑器控件使用相同的绑定方法。是的,它与ToDo相同,但在加载页面时抛出异常。我刚刚尝试过这个-userAddress.SetBinding(Editor.TextProperty,newbinding(“Address”,BindingMode.TwoWay));,这仍然不会更新编辑器,但也不会抛出异常——我想我已经接近了——收回这一点,它确实有效——使用这个userAddress.SetBinding(editor.TextProperty,new Binding(“Address”,BindingMode.TwoWay));谢谢你,杰森,为我指引了正确的方向