ViewModel构造崩溃应用程序

时间:2018-12-24 15:31:09

标签: c# xamarin viewmodel

我正在尝试通过与Notes(一对多)联系来学习Xamarin表单。 我用一个ContactsViewModel绑定很好地完成了ListView。

所以现在我要制作一个编辑上下文按钮:

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

感谢您的关注,我也欢迎任何对代码的批评和改进。

删除参数时可以使用,但是当我只输入一个参数时就会崩溃。

1 个答案:

答案 0 :(得分:1)

检查以确保将有效值传递给页面。

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();
        }
    }
}