Xamarin.Forms-PropertyChanged集{}未调用 [英] Xamarin.Forms - PropertyChanged set {} not called
问题描述
我有一个简单的 LoginView 和 LoginViewModel . 首先,这是View的相关代码:
I've got a simple LoginView and LoginViewModel. First, here is the related code of the View:
<StackLayout>
<Entry
Text="{Binding Email, Mode=TwoWay}"
/>
...
这是ViewModel:
And here is the ViewModel:
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)));
}
}
...
}
如果我在构造函数中说类似
If I say inside the Constructor something like
Email = "abc";
该条目肯定显示值"abc".但是,如果我更改条目中的文本,则不会触发 set {} ,因此 PropertyChanged()也不会触发.
the Entry for sure displays the value "abc". But if I change the Text inside the Entry, the set {} is not firing so the PropertyChanged() also does not.
我在这里错过了什么吗?还是必须使用BindableProperties?
Do I miss something here or do I have to use BindableProperties?
提前谢谢!
对于需要为 LoginView 定义BindingContext的任何人,下面是代码背后:
For anyone needing the definition of the BindingContext for LoginView, here is the Code-Behind:
[XamlCompilation(XamlCompilationOptions.Compile)]
public sealed partial class LoginView
{
public LoginView()
{
InitializeComponent();
// Set the ViewModel
this.BindingContext = new LoginViewModel();
}
}
编辑2
因此,我创建了一个Testproject,该项目只是将条目的TextProperty绑定到属性.这行得通!然后,我编辑了我现有的代码(删除了基类,简化了所有内容,等等)以简单地完成相同的基本操作……而且它不起作用.这可能是什么?
Edit 2
So I created a Testproject which simply binds the TextProperty of a Entry to a Property. This works! Then I edited my existing Code (removed Baseclasses, simplified everything, etc) to simply do the same basic thing... And it doesn't work. What could this be?
推荐答案
更新Xamarin.Forms版本后,该错误消失了!
After I updated my Xamarin.Forms version, the bug now is gone!
这篇关于Xamarin.Forms-PropertyChanged集{}未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!