我正在尝试通过与笔记联系(一对多)来学习 Xamarin 表单。我已经完成了一个列表视图,其中联系人视图模型绑定运行良好。
所以现在我正在尝试制作一个编辑上下文按钮:
public void OnEdit(object sender, EventArgs e)
{
Contact contact = ((MenuItem)sender).CommandParameter as Contact;
Navigation.PushAsync(new ContactPage(contact));
}
所以在这里我将我的联系人发送到带有基本条目的页面,这个联系人页面的构造函数:
public ContactPage(Contact contact)
{
InitializeComponent();
BindingContext = new ContactViewModel()
{
Id = contact.Id,
Firstname = contact.Firstname,
Lastname = contact.Lastname,
Email = contact.Email,
Address = contact.Address,
Notes = contact.Notes
};
}
但是当我单击我的编辑上下文按钮时,我得到一个应用程序崩溃,我不明白为什么。
如果您需要更深入地了解代码,这里是存储库:https://github.com/yerffeog/Contactium
感谢您的关注,我也愿意接受任何代码批评和改进。
删除参数时它可以工作,但是当我只放置一个参数时,我就会崩溃。
检查以确保将有效值传递到页面。
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();
}
}
}