简体   繁体   中英

trouble Instantiating Model Class Within ViewModel Class C#

I am trying to instantiate an object of my model class, from my MVVM test project, in my viewmodel class.

Person Model Class:

using System.ComponentModel;

namespace WPFAppTest.Models
{
    public class Person : INotifyPropertyChanged
    {
        private string _FirstName;
        public string FirstName
        {
            get
            {
                return _FirstName;
            }
            set
            {
                _FirstName = value;
                RaisePropertyChange("FirstName");
                RaisePropertyChange("FullName");
            }
        }
        private string _LastName;
        public string LastName
        {
            get
            {
                return _LastName;
            }
            set
            {
                _LastName = value;
                RaisePropertyChange("LastName");
                RaisePropertyChange("FullName");
            }
        }
        public string FullName
        {
            get
            {
                return _FirstName + " " + _LastName;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChange(string property)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
        }

    }
}

PersonViewModel Class:

using WPFAppTest.Models;

namespace WPFAppTest.ViewModels
{
    public class PersonViewModel
    {
        public Person person = new Person();

        person.FirstName = "Iain";
    }
}

It seems I get an error saying "The name 'person.FirstName' does not exist in the current context".

How do I create the object and then access its properties to set the values? Which in turn I can then use for data bindings in my View?

The statement person.FirstName = "Iain"; must be inside a method or in the constructor

public class PersonViewModel
{
    public Person person = new Person();

    public PersonViewModel() // constructor
    {
        person.FirstName = "Iain";
    }

    public void Test() // method
    {
        person.FirstName = "Pete";
    }
}

Note: the constructor which is called automatically when you create an object with new PersonViewModel() has the same name as the class and does not have a return type (there is no void keyword).

The method Test must be called explicitly

var p = new PersonViewModel(); // calls constructor and assigns "Iain".
p.Test(); // Assigns "Pete"

The constructor can have parameters like a method.

public PersonViewModel(string personName) // constructor with parameter
{
    person.FirstName = personName;
}

You must pass an argument to this parameter when calling new :

var p = new PersonViewModel("Sue"); // Creates a VM with a person named "Sue".

For a View Model it would probably make sense to pass a Person instead.

public class PersonViewModel
{
    private readonly Person _person;

    public string FirstName => _person.FirstName;

    public PersonViewModel(Person person) // constructor
    {
        _person = person;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM