У меня есть представление, которое заполняется двумя разными моделями .Для моего приложения это означает, что две разницы Службы используются для получения данных для этих соответствующих моделей.
Теперь данные просто не будут заполняться.Что я имею в виду?В настоящее время при загрузке страницы отображается только Group.Name
.Остальные метки User
не отображаются.Мне нужно создать кнопку, которая изменяет поле в User
объекте с привязкой до того, как отобразятся три метки.
Примечание: BaseViewModel
наследует INotifyPropertyChanged
и IPageLifecycleAware
.SetProperty()
вызывает событие, когда свойство изменяется.
Просмотр
<StackLayout Margin="10" VerticalOptions="StartAndExpand">
<Grid>
<Label Grid.Row="0" Text="{Binding User.Name}"/>
<Label Grid.Row="1" Text="{Binding User.Age}"/>
<Label Grid.Row="2" Text="{Binding User.Occupation}"/>
<Label Grid.Row="3" Text="{Binding Group.Name}"/>
</Grid>
</StackLayout>
ViewModel
public class ProfileViewModel : BaseViewModel
{
private IGroupService _groupService;
private IUserService _userService;
public ProfileViewModel(IGroupService groupService, IUserService userService)
{
_groupService = groupService;
_userService = userService;
GetUser();
GetGroup();
}
private async void GetUser()
{
var user = await _userService.GetUserAsync();
User = user;
SetProfilePhoto();
}
private async void GetGroup()
{
var group = await _groupService.GetGroupAsync();
Group = group;
}
private void SetProfilePhoto()
{
var profilePhotoBytes = User.ProfilePhotoBytes;
if (profilePhotoBytes == null || profilePhotoBytes.Length == 0)
ProfilePhoto = ImageSource.FromResource("MyApp.Images.default_profile_picture.png");
else
ProfilePhoto = ImageSource.FromStream(() => new MemoryStream(profilePhotoBytes));
}
private ImageSource _profilePhoto;
public ImageSource ProfilePhoto { get => _profilePhoto; set => SetProperty(ref _profilePhoto, value); }
private User _user;
public User User { get => _user; set => SetProperty(ref _user, value);
}
private Group _group;
public Group Group{ get => _group; set => SetProperty(ref _group, value); }
}
Модели
public class User: BindableBase
{
private string _id;
public string Id { get => _id; set => SetProperty(ref _id, value); }
private string _name;
public string Name { get => _name; set => SetProperty(ref _name, value); }
private string _age;
public string Age { get => _age; set => SetProperty(ref _age, value); }
private string _occupation;
public string Occupation { get => _occupation; set => SetProperty(ref _occupation, value); }
private byte[] _profilePhotoBytes;
public byte[] ProfilePhotoBytes { get => _profilePhotoBytes; set => SetProperty(ref _profilePhotoBytes, value); }
private ImageSource _profilePhoto;
public ImageSource ProfilePhoto { get => _profilePhoto; set => SetProperty(ref _profilePhoto, value); }
}
public class Group: BindableBase
{
private string _id;
public string Id { get => _id; set => SetProperty(ref _id, value); }
private string _name;
public string Name { get => _name; set => SetProperty(ref _name, value); }
private byte[] _groupPhotoBytes;
public byte[] GroupPhotoBytes { get => _groupPhotoBytes; set => SetProperty(ref _groupPhotoBytes, value); }
private string _createdUserId;
public string CreatedUserId { get => _createdUserId; set => SetProperty(ref _createdUserId, value); }
private DateTime _createdDate;
public DateTime CreatedDate { get => _createdDate; set => SetProperty(ref _createdDate, value); }
}