【问题标题】:xamarin.form binding twoway for object properties not working对象属性的 xamarin.form 双向绑定不起作用
【发布时间】:2018-09-16 20:26:39
【问题描述】:
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(); } }

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

【问题讨论】:

    标签: xamarin mvvm binding xamarin.forms model


    【解决方案1】:
        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;
    

    在您的视图中尝试此操作以更改属性

    RaisePropertyChanged(()=&gt;YourPropertyName);

    【讨论】:

      【解决方案2】:

      您必须实现 INotifyPropertyChanged,它是双向绑定的主接口。在您的视图模型类中,您必须以这种方式实现

       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));  
          }
      } 
      

      在你的 MainPage()

      public MainPage() {  
              InitializeComponent();  
              vm = new ContactViewModel();  
              BindingContext = vm;  
          }        
      

      【讨论】:

      • 感谢 delta12 是的,正如您所写,我已经实施了 inotifypropertyChanged。主要问题是双向绑定。一种绑定方式做得很好。我想要双向绑定,但它不起作用
      • 你应用了PropertyChangedEventHandler
      • 请粘贴您的 ContactViewModel 的详细代码
      • 公共类 ContactViewModel : INotifyPropertyChanged { public ContactViewModel(){ } 公共事件 PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } 私人联系人 _selectedContact;公共联系人 SelectedContact { get { return _selectedContact; } 设置 { _selectedContact = 值; OnPropertyChanged(); } }}
      猜你喜欢
      • 1970-01-01
      • 2023-03-18
      • 2012-02-14
      • 2020-11-26
      • 2012-12-17
      • 2014-04-10
      • 2011-10-14
      相关资源
      最近更新 更多