【发布时间】:2021-04-14 22:58:27
【问题描述】:
我正在尝试从我的视图模型中的对象更新我的 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);
}
【问题讨论】:
-
首先,如果您只有一个 User 对象,为什么还要使用 BindableLayout 和 DataTemplate?其次,你的 VM 和相关对象是否实现了 INotifyPropertyChanged?
-
是的,确实如此。是的,我不需要可绑定的布局。
标签: c# forms xamarin data-binding