我有一个简单的LoginView和LoginViewModel。 首先,这里是视图的相关代码:
<StackLayout>
<Entry
Text="{Binding Email, Mode=TwoWay}"
/>
...
这是视图模型:
public class LoginViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private string _email;
public string Email
{
get => _email;
set
{
_email = value;
PropertyChanged(this, new PropertyChangedEventArgs(nameof(Email)));
}
}
...
}
如果我在构造函数中说类似
Email = "abc";
条目肯定显示值"abc"。但是如果我更改条目中的文本,则集合 {}不会触发,因此PropertyChanged((也不会触发。
我在这里错过了什么还是必须使用可绑定属性?
提前感谢!
编辑 1
对于任何需要定义BindingContext for LoginView的人,下面是代码隐藏:
[XamlCompilation(XamlCompilationOptions.Compile)]
public sealed partial class LoginView
{
public LoginView()
{
InitializeComponent();
// Set the ViewModel
this.BindingContext = new LoginViewModel();
}
}
编辑 2
所以我创建了一个测试项目,它只是将条目的 TextProperty 绑定到属性。这行得通!然后我编辑了我现有的代码(删除了基类,简化了所有内容等(,以简单地做同样的基本事情......而且它不起作用。这是什么?
在我更新了我的Xamarin.Forms版本后,现在的错误已经消失了!