简体   繁体   中英

How to use C# WPF?

For my school assignment I have to make a reservationsystem for a hotel. The thing is that I have to make the code without the UI (never done this before). I have to add the UI later. Each UI should be able to be used with my code.

Now I have a class called Secretary The Secretary is able to make a Reservation . I have this method in the class Secretary :

public void CheckIn()
        {
            Reservation reservation = new Reservation();
            reservation.ReservationDate1 = //info from a textbox
        }

Now I know that I should connect everything when my UI is ready, but what is the best way to tell my code that he should get the information from the textbox when the textbox isn't there yet???

i would suggest you start by reading this

now as for what you have to do then first you will need to model your data

public class Reservation
{
    public DateTime Date{get;set;}
    public string Name{get;set;}

    public void Save(){/*Copy entry to DB, webservice, file, etc*/}
    public void Delete(){/*delete entry from DB, webservice, file, etc*/}
    //ect

}

as you can see you now have a list of what is required for a reservation, and functionality that will persist your data

next you need a ViewModel

public class ReservationViewModel:INotifyPropertyCHanged
{
    public Reservation Reservation{get;set;} //Link to model
    private DateTime _Date;

    public DateTime Date
    {
        get { return _Date; }
        set { SetProperty(ref _Date, value); }
    }
    private string _Name;

    public string Name
    {
        get { return _Name; }
        set { SetProperty(ref _Name, value); }
    }
    public void SetProperty<T>(ref T store, T value,[CallerMemberName] string name = null)
    {
        store = value;
        if(PropertyChanged!=null)PropertyChanged(this,new PropertyChangedArgs(name);
    }
    public void Save(){/*validate, copy over model values call models save*/}
    public void Cancel(){/*change VM values back to Model values*/}
    public void Delete(){/*validate, call models delete*/}
    //ect
}

at this point you can stop as you have defined the data and behaviour of the system, though i would suggest adding a testing project to run your code and check it works

when you get to your View you would just bind to your ViewModel and the rest is done for you

<TextBox Text={Binding Name}/>

You can use MVVM with a ViewModel , but if you just want a method ready to accept input when you design the UI, you can make Checkin() take a string parameter so it's Checkin(string value) and assign value to ReservationDate1 .

        public void CheckIn(string val)
        {
            Reservation reservation = new Reservation();
            reservation.ReservationDate1 = val;
        }

This is an exercise in keeping your logic and your UI nice and separate. A little more tightly coupled, but doable, would be this:

        public void CheckIn(TextBox tb)
        {
            Reservation reservation = new Reservation();
            reservation.ReservationDate1 = tb.Text;
        }

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