【发布时间】:2020-01-26 04:22:51
【问题描述】:
我有一个由两个不同的模型填充的视图。对于我的应用,这意味着两个不同的 Services 用于获取这些各自模型的数据。
现在数据根本不会填充。我是什么意思?目前,页面加载时仅显示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>
视图模型
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); }
}
【问题讨论】:
-
更新: 我刚刚意识到,当我在 else 语句中通过
bytes设置注释掉ProfilePhotoBytes时,这解决了问题。我已经测试了 15 次以确保它是一致的。这意味着该操作会以某种方式干扰被通知给前端的User对象的其余部分。请问有什么帮助吗? -
请下断点调试profilePhotoBytes是否获取到值。
-
它肯定会得到一个值,因为照片被设置,然后文本没有;'t。但是,如果它转到默认照片,那么照片和文字都会显示。我注意到较低分辨率的照片没有这个问题。它很有趣。
标签: c# xaml xamarin xamarin.forms