Home > Enterprise >  How to pass time value from page.cs to ViewModel
How to pass time value from page.cs to ViewModel

Time:10-04

How to paas time value from page.cs to ViewModel.

timevalue is string like "14h:15m"

Here I'm trying...

Page.cs

_model.StartTime = int.Parse(timevalue);

ViewModel

public DateTime StartTime { get; set; } = DateTime.Today; 
public DateTime EndTime { get; set; }
public int Duration { get; set; } 
private TimeSpan[] _StartTimeValues { get; set; }
private int _StartTimeIndex = -1;
public int StartTime
{
    get { return _StartTime; }
    set
    {
        _StartTimeIndex = value;

        if (value >= 0)
        {
            StartTime =StartTime.Date.Add(_StartTimeValues[value]); 
            EndTime = StartTime.AddMinutes(Duration);                                   
        }       
        OnPropertyChanged(nameof(StartTime));                
    }
}

CodePudding user response:

At first, please change the _StartTimeIndex's type to SpanTime, because the TimeDate.Add need a parameter type of SpanTime. Such as:

  string time = "12h45m";
  char[] hourandminute = new char[] { 'h', 'm' };
  string[] temp = time.Split(hourandminute);
  int hour = int.Parse(temp[0]);
  int minutes = int.Parse(temp[1]);
  TimeSpan timeSpan = new TimeSpan(hour, minutes, 0);
  • Related