对象属性的xamarin.form双向绑定无效

对象属性的xamarin.form双向绑定无效,xamarin,mvvm,binding,xamarin.forms,model,Xamarin,Mvvm,Binding,Xamarin.forms,Model,单向绑定正在工作,但我想要双向绑定。如果我更改文本或修改firstname条目中的文本,那么它应该更改SelectedContact的FNAME属性您必须实现INotifyPropertyChanged,这是双向绑定的主界面。在viewmodel类中,您必须以这种方式实现 myview <Image Grid.Column="0" Grid.Row="0" Source="contactIcon.png" /> <Entry Grid.Column="1" Grid.Row="

单向绑定正在工作,但我想要双向绑定。如果我更改文本或修改firstname条目中的文本,那么它应该更改SelectedContact的FNAME属性

您必须实现INotifyPropertyChanged,这是双向绑定的主界面。在viewmodel类中,您必须以这种方式实现

myview
<Image Grid.Column="0" Grid.Row="0" Source="contactIcon.png" />
<Entry Grid.Column="1" Grid.Row="0"  Text="{Binding 
SelectedContact.FNAME,Mode=TwoWay}" Placeholder="First Name" />
<Entry Grid.Column="1" Grid.Row="1"  Text="{Binding 
SelectedContact.LNAME,Mode=TwoWay}" Placeholder="Last Name"/>
<Image Grid.Column="0" Grid.Row="2" Source="calIcon.png" />
<Entry Grid.Column="1" Grid.Row="2" Text="{Binding 
SelectedContact.PHONE,Mode=TwoWay}" Placeholder="Mobile" 
Keyboard="Telephone"/>
<Image Grid.Column="0" Grid.Row="3" Source="emailIcon.png" />
<Entry Grid.Column="1" Grid.Row="3" Text="{Binding 
SelectedContact.EMAIL,Mode=TwoWay}" Placeholder="Email" Keyboard="Email"/>
<Entry Grid.Column="1" Grid.Row="4" Text="{Binding 
SelectedContact.BALANCE,Mode=TwoWay}" Placeholder="Email" 
Keyboard="Email"/>

Contact model
    public int ID { get; set; }
    public string FNAME { get; set; }
    public string LNAME { get; set; }
    public string PHONE { get; set; }
    public string EMAIL { get; set; }
    public Double BALANCE { get; set; }

ContactViewModel
private Contact _selectedContact;
public Contact SelectedContact{get { return _selectedContact; }set{ 
_selectedContact = value; OnPropertyChanged(); } }
在你的主页上

 public class ContactViewModel: INotifyPropertyChanged 
 { 
   private Contact _selectedContact;
   public Contact SelectedContact
   { 
      get 
       { return _selectedContact; }
      set
       { _selectedContact = value;
        OnPropertyChanged(); 
       } 
    }
  public event PropertyChangedEventHandler PropertyChanged;  
  protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) {  
        PropertyChanged ? .Invoke(this, new PropertyChangedEventArgs(propertyName));  
    }
} 
在您的视图中尝试此操作以更改属性


RaisePropertyChanged=>YourPropertyName

谢谢delta12是的,我已经插入了InotifyProperty,如您所写。主要问题是双向绑定。一种方法是做得很好。我想要双向绑定,但该绑定不起作用您是否应用了PropertyChangedEventHandler请粘贴ContactViewModel公共类ContactViewModel的详细代码:INotifyPropertyChanged{public ContactViewModel{}公共事件PropertyChangedEventHandler PropertyChanged;受保护的虚拟无效OnPropertyChanged[CallerMemberName]字符串propertyName=null{PropertyChanged?.Invokethis,new PropertyChangedEventHandler propertyName;}私人联系人\u selectedContact;公共联系人selectedContact{get{return}selectedContact;}set{selectedContact=value;OnPropertyChanged;}}
public MainPage() {  
        InitializeComponent();  
        vm = new ContactViewModel();  
        BindingContext = vm;  
    }        
    protected void RaisePropertyChanged(String property)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

    protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
    {
        MemberExpression expression = propertyExpression.Body as MemberExpression;
        RaisePropertyChanged(expression.Member.Name);
    }


    public event PropertyChangedEventHandler PropertyChanged;