Убедитесь, что на страницу передается правильное значение.
public async void OnEdit(object sender, EventArgs e) {
Contact contact = ((MenuItem)sender).CommandParameter as Contact;
if(contact != null) {
var contactPage = new ContactPage(contact);
await Navigation.PushAsync(contactPage);
}
}
На основе предоставленного кода хранилища,
Эта модель просмотра
public class ContactViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public ContactViewModel()
{
}
/// <summary>
/// Event when property changed.
/// </summary>
/// <param name="s">string</param>
void OnPropertyChanged(string s)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(s));
}
/// <summary>
/// Id of the contact.
/// </summary>
public int Id {
get
{
return Id;
}
set
{
Id = value;
OnPropertyChanged(nameof(Id));
}
}
/// <summary>
/// Lastname of the contact.
/// </summary>
public string Lastname {
get
{
return Lastname;
}
set
{
Lastname = value;
OnPropertyChanged(nameof(Lastname));
}
}
/// <summary>
/// Firstname of the contact.
/// </summary>
public string Firstname {
get
{
return Firstname;
}
set
{
Firstname = value;
OnPropertyChanged(nameof(Firstname));
}
}
/// <summary>
/// Email of the contact.
/// </summary>
public string Email {
get
{
return Email;
}
set
{
Email = value;
OnPropertyChanged(nameof(Email));
}
}
/// <summary>
/// Address of the contact.
/// </summary>
public string Address
{
get
{
return Address;
}
set
{
Address = value;
OnPropertyChanged(nameof(Address));
}
}
/// <summary>
/// Notes of the contact.
/// </summary>
public List<Note> Notes
{
get
{
return Notes;
}
set
{
Notes = value;
OnPropertyChanged(nameof(Notes));
}
}
}
Вышеприведенный код потерпит неудачу, так как свойства ссылаются на себя, что приведет к переполнению стека и аварийному завершению приложения.
Чтобы избежать повторения кода, создайте базовую модель представления с основами
public class ViewModelBase : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Event when property changed.
/// </summary>
/// <param name="s">string</param>
protected void OnPropertyChanged([CallerMemberName] string member = "") {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(member));
}
}
Оттуда убедитесь, что модель представления имеет вспомогательные поля для открытых свойств.
public class ContactViewModel : ViewModelBase {
private string id;
/// <summary>
/// Id of the contact.
/// </summary>
public int Id {
get { return id; }
set {
id = value;
OnPropertyChanged();
}
}
string lastname;
/// <summary>
/// Lastname of the contact.
/// </summary>
public string Lastname {
get { return lastname; }
set {
lastname = value;
OnPropertyChanged();
}
}
string firstname
/// <summary>
/// Firstname of the contact.
/// </summary>
public string Firstname {
get { return firstname; }
set {
firstname = value;
OnPropertyChanged();
}
}
string email;
/// <summary>
/// Email of the contact.
/// </summary>
public string Email {
get { return email; }
set {
email = value;
OnPropertyChanged();
}
}
string address;
/// <summary>
/// Address of the contact.
/// </summary>
public string Address {
get { return address; }
set {
address = value;
OnPropertyChanged();
}
}
string notes;
/// <summary>
/// Notes of the contact.
/// </summary>
public List<Note> Notes {
get { return notes; }
set {
notes = value;
OnPropertyChanged();
}
}
}