ViewModel 构建崩溃的应用程序
ViewModel construction crashing application
我正在尝试通过联系笔记(一对多)来学习 Xamarin Forms。
我已经完成了一个 ListView 与 ContactsViewModel 绑定,效果很好。
所以现在我正在尝试制作一个编辑上下文按钮:
public void OnEdit(object sender, EventArgs e)
{
Contact contact = ((MenuItem)sender).CommandParameter as Contact;
Navigation.PushAsync(new ContactPage(contact));
}
所以在这里我将我的联系人发送到具有基本条目的页面,这个 ContactPage 的构造函数:
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();
}
}
}
我正在尝试通过联系笔记(一对多)来学习 Xamarin Forms。 我已经完成了一个 ListView 与 ContactsViewModel 绑定,效果很好。
所以现在我正在尝试制作一个编辑上下文按钮:
public void OnEdit(object sender, EventArgs e)
{
Contact contact = ((MenuItem)sender).CommandParameter as Contact;
Navigation.PushAsync(new ContactPage(contact));
}
所以在这里我将我的联系人发送到具有基本条目的页面,这个 ContactPage 的构造函数:
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();
}
}
}