xamarin 如何将时间值从page.cs传递到ViewModel

hc8w905p  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(128)

如何将时间值从page.cs放置到ViewModel。
timevalue是类似"14h:15m"的字符串
我在试着...
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));                
    }
}
hwazgwia

hwazgwia1#

首先,请将_StartTimeIndex的类型改为SpanTime,因为TimeDate.Add需要一个参数类型为SpanTime.例如:

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

相关问题