问题描述
我正在尝试从我的视图模型中的对象更新我的 xamarin 绑定。我究竟做错了什么。 当我查看页面并查看“用户”时,我看到我的用户对象在那里。
页面:
<StackLayout BindableLayout.ItemsSource="{Binding User}"
Orientation="Horizontal">
<BindableLayout.ItemTemplate>
<DataTemplate x:DataType="model:User">
<StackLayout>
<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>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
你需要了解的关于带有 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);
}
解决方法
根据 Jason 的观点,如果只有一个 User 对象,则不需要使用 BindableLayout 和 DataTemplate,直接将 User 对象绑定到 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>
如果你有 IEnumerable User 的集合,你可以使用 BindableLayout.ItemsSource 和一个 DataTemplate
您需要为用户属性实现 INotifyPropertyChanged,它会通知数据更改。
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}" />
</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");
}
}
}