C# Can';t从Xamarin表单中的对象更新数据绑定
我试图从viewmodel中的对象更新我的xamarin绑定。我做错了什么。 当我看到页面和“用户”时,我看到我的用户对象就在那里 第页:C# Can';t从Xamarin表单中的对象更新数据绑定,c#,forms,xamarin,data-binding,C#,Forms,Xamarin,Data Binding,我试图从viewmodel中的对象更新我的xamarin绑定。我做错了什么。 当我看到页面和“用户”时,我看到我的用户对象就在那里 第页: 关于使用mvvhelpers的Viewmodel,您需要了解什么 public User User { get; set; } public MyProfileViewModel() { Title = "MyProfile"; Ref
关于使用mvvhelpers的Viewmodel,您需要了解什么
public User User { get; set; }
public MyProfileViewModel()
{
Title = "MyProfile";
RefreshCommand = new AsyncCommand(Refresh);
AddCommand = new AsyncCommand(Add);
// RemoveCommand = new AsyncCommand<User>(Remove);
User = new User();
}
public async refrsh(){
User = await MyProfileService.GetMyProfile(2);
nameDisplay = User.Name;
}
string nameDisplay;
public string NameDisplay
{
get => nameDisplay;
set => SetProperty(ref nameDisplay, value);
}
公共用户{get;set;}
公共MyProfileViewModel()
{
Title=“MyProfile”;
刷新命令=新的异步命令(刷新);
AddCommand=新的异步命令(Add);
//RemoveCommand=新的异步命令(Remove);
User=新用户();
}
公共异步refrsh(){
User=wait MyProfileService.GetMyProfile(2);
namesplay=User.Name;
}
字符串名称显示;
公共字符串名称显示
{
get=>nameDisplay;
set=>SetProperty(参考名称显示,值);
}
根据Jason的观点,如果您只有一个用户对象,则不需要使用BindableLayout和DataTemplate,您可以直接将用户对象绑定到StackLayout BindingContext
<StackLayout BindingContext="{Binding User}">
<Label Text="{Binding Name, Mode=OneWay}" />
<Label Text="{Binding PhoneNumber, Mode=OneWay}" />
<Label Text="{Binding Adress, Mode=OneWay}" />
<Label Text="{Binding Email, Mode=OneWay}" />
</StackLayout>
完整代码:
<StackLayout>
<StackLayout BindingContext="{Binding User}">
<Label Text="{Binding Name, Mode=OneWay}" />
<Label Text="{Binding PhoneNumber, Mode=OneWay}" />
<Label Text="{Binding Adress, Mode=OneWay}" />
<Label Text="{Binding Email, Mode=OneWay}" />
</StackLayout>
<Button
x:Name="btn1"
Command="{Binding changecommand}"
Text="change user data" />
</StackLayout>
public partial class Page27 : ContentPage
{
public Page27()
{
InitializeComponent();
this.BindingContext = new MyProfileViewModel();
}
}
public class MyProfileViewModel
{
public User User { get; set;}
public ICommand changecommand { get; }
public MyProfileViewModel()
{
User = new User();
User.Name = "cherry";
User.PhoneNumber = "123";
User.Adress = "location 1";
User.Email = "xxxxx.@outlook.com";
changecommand = new Command(() =>
{
User.Name = "barry";
});
}
}
public class User:ViewModelBase
{
private string _Name;
public string Name
{
get { return _Name; }
set
{
_Name = value;
RaisePropertyChanged("Name");
}
}
private string _PhoneNumber;
public string PhoneNumber
{
get { return _PhoneNumber; }
set
{
_PhoneNumber = value;
RaisePropertyChanged("PhoneNumber");
}
}
private string _Adress;
public string Adress
{
get { return _Adress; }
set
{
_Adress = value;
RaisePropertyChanged("Adress");
}
}
private string _Email;
public string Email
{
get { return _Email; }
set
{
_Email = value;
RaisePropertyChanged("Email");
}
}
}
公共部分类第27页:内容页
{
公共页27()
{
初始化组件();
this.BindingContext=新的MyProfileViewModel();
}
}
公共类MyProfileViewModel
{
公共用户{get;set;}
公共ICommand changecommand{get;}
公共MyProfileViewModel()
{
User=新用户();
User.Name=“cherry”;
User.PhoneNumber=“123”;
User.address=“位置1”;
User.Email=“xxxxx@outlook.com”;
changecommand=新命令(()=>
{
User.Name=“barry”;
});
}
}
公共类用户:ViewModelBase
{
私有字符串\u名称;
公共字符串名
{
获取{return\u Name;}
设置
{
_名称=值;
RaiseProperty变更(“名称”);
}
}
私人字符串(电话号码),;
公共字符串电话号码
{
获取{return\u PhoneNumber;}
设置
{
_PhoneNumber=值;
RaisePropertyChanged(“电话号码”);
}
}
私有字符串地址;
公共字符串地址
{
获取{return\u address;}
设置
{
_地址=值;
RaiseProperty变更(“地址”);
}
}
私人字符串\u电子邮件;
公共字符串电子邮件
{
获取{return\u Email;}
设置
{
_电子邮件=价值;
RaisePropertyChanged(“电子邮件”);
}
}
}
首先,如果只有一个用户对象,为什么要使用BindableLayout和DataTemplate?其次,您的VM和相关对象是否实现了INotifyPropertyChanged?是的,它们实现了。是的,我不需要可绑定的布局。@KonradUIV有什么更新吗?我的回答对你有帮助吗?
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
<StackLayout>
<StackLayout BindingContext="{Binding User}">
<Label Text="{Binding Name, Mode=OneWay}" />
<Label Text="{Binding PhoneNumber, Mode=OneWay}" />
<Label Text="{Binding Adress, Mode=OneWay}" />
<Label Text="{Binding Email, Mode=OneWay}" />
</StackLayout>
<Button
x:Name="btn1"
Command="{Binding changecommand}"
Text="change user data" />
</StackLayout>
public partial class Page27 : ContentPage
{
public Page27()
{
InitializeComponent();
this.BindingContext = new MyProfileViewModel();
}
}
public class MyProfileViewModel
{
public User User { get; set;}
public ICommand changecommand { get; }
public MyProfileViewModel()
{
User = new User();
User.Name = "cherry";
User.PhoneNumber = "123";
User.Adress = "location 1";
User.Email = "xxxxx.@outlook.com";
changecommand = new Command(() =>
{
User.Name = "barry";
});
}
}
public class User:ViewModelBase
{
private string _Name;
public string Name
{
get { return _Name; }
set
{
_Name = value;
RaisePropertyChanged("Name");
}
}
private string _PhoneNumber;
public string PhoneNumber
{
get { return _PhoneNumber; }
set
{
_PhoneNumber = value;
RaisePropertyChanged("PhoneNumber");
}
}
private string _Adress;
public string Adress
{
get { return _Adress; }
set
{
_Adress = value;
RaisePropertyChanged("Adress");
}
}
private string _Email;
public string Email
{
get { return _Email; }
set
{
_Email = value;
RaisePropertyChanged("Email");
}
}
}